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

后端 未结 2 927
小鲜肉
小鲜肉 2020-12-30 03:14

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

相关标签:
2条回答
  • 2020-12-30 03:53

    I know this post is 2 years old, but a better solution would be to use Fluent API to set the foreign key (rather than using [ForeignKey] attribute in your Customer class. Here is how you would do it:

    public class Customer
    {
        public int CustomerID { get; set; }
        public virtual ApplicationUser ApplicationUser { get; set; }
    }
    
    public class ApplicationUser : IdentityUser
    {
        public virtual Customer Customer { get; set; }
    }
    
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext() : base("DefaultConnection")
        {
        }
    
        public DbSet<Customer> Customers { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            // one-to-zero or one relationship between ApplicationUser and Customer
            // UserId column in Customers table will be foreign key
            modelBuilder.Entity<ApplicationUser>()
                .HasOptional(m => m.Customer)
                .WithRequired(m => m.ApplicationUser)
                .Map(p => p.MapKey("UserId"));
        }
    
    }
    

    This would create a UserId column in your Customers table that is a foreign key to AspNetUsers table. You can omit .Map(p => p.MayKey("UserId")) and EF will name the foreign key ApplicationUser_Id by convention.

    0 讨论(0)
  • 2020-12-30 03:57

    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; }
    }
    
    0 讨论(0)
提交回复
热议问题