AspNetUsers' ID as Foreign key in separate table, one-to-one relationship

JayEz picture JayEz · Sep 17, 2014 · Viewed 20.7k times · Source

I have looked up and down, tried all the different and various ways of being able to store a foreign key of the AspNetUser table in a separate Customer table. I'm still new at ASP.NET and the Entity Framework, but I've read quite a few posts and documentations.

Currently this is what I have

MODELS

public class Customer
{
    [Display (Name="Customer ID")]
    public int CustomerID { get; set; }

    public string UserId { get; set; }
    [ForeignKey("UserId")]
    public virtual ApplicationUser ApplicationUser { get; set; }

}


 public class ApplicationUser : IdentityUser
{
    public virtual Customer Customer { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<Customer> Customers { get; set; }

    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

}

I get this error, quote

Unable to determine the principal end of an association between the types 'TestApplication.Models.Customer' and 'TestApplication.Models.ApplicationUser'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

I also tried this person's method found here: The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations

So I commented out the ForeignKey annotations and used the person's suggestion, using the "modelBuilder" approach. And when I updated my database, the 'Id' from the AspNetUsers table was in the Customers table (which is good), but the CustomerID as a ForeignKey was also in the AspNetUsers table, which is not what I want.

What I want, is the AspNetUsers' 'Id' to be in the Customers table as a ForeignKey.

Answer

Andreas &#197;gren picture Andreas Ågren · Sep 17, 2014

In a one-to-one relation the "child" table, in your case Customer, should have the same primary key as the related table, i.e. the foreign key.

The code sample you have supplied means that, in Customer you will have a PK named CustomerID which is different from UserId.

This should work in your case (untested):

public class Customer
{
    [Key]
    public string UserId { get; set; }

    [ForeignKey("UserId")]
    public virtual ApplicationUser ApplicationUser { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public virtual Customer Customer { get; set; }
}

Edit:

MSDN for ForeignKeyAttribute states:

If you add the ForeigKey attribute to a foreign key property, you should specify the name of the associated navigation property. If you add the ForeigKey attribute to a navigation property, you should specify the name of the associated foreign key(s).

I interpret this as that it should be possible to add the ForeignKey-attribute to either the navigation property or the foreign key property, and that either way should work, but apparently not. Moving it as per below should do the trick.

public class Customer
{
    [Key, ForeignKey("ApplicationUser")]
    public string UserId { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public virtual Customer Customer { get; set; }
}