小编Jay*_*yEz的帖子

AspNetUsers的ID作为单独表中的外键,一对一的关系

我上下打量,尝试了所有不同的方法,能够将AspNetUser表的外键存储在一个单独的Customer表中.我还是ASP.NET和实体框架的新手,但我已经阅读了不少帖子和文档.

目前这就是我所拥有的

楷模

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")
    {
    }

}
Run Code Online (Sandbox Code Playgroud)

我引用了这个错误

无法确定类型"TestApplication.Models.Customer"和"TestApplication.Models.ApplicationUser"之间关联的主要结尾.必须使用关系流畅API或数据注释显式配置此关联的主要结尾.

我也试过这个人的方法:必须使用关系流畅的API或数据注释显式配置此关联的主要结尾

所以我注释掉了ForeignKey注释并使用了人的建议,使用了"modelBuilder"方法.当我更新我的数据库时,AspNetUsers表中的'Id'在Customers表中(这很好),但CustomerID作为ForeignKey也在AspNetUsers表中,这不是我想要的.

我想要的是AspNetUsers的'Id'作为ForeignKey在Customers表中.

c# database asp.net entity-framework one-to-one

16
推荐指数
2
解决办法
2万
查看次数

标签 统计

asp.net ×1

c# ×1

database ×1

entity-framework ×1

one-to-one ×1