C# EF代码首先添加它';在我定义的FK旁边有自己的FK
我有两个类,从Identity 2.0扩展而来的applicationuser和我自己的对象。我已经定义了我自己的外键,但是当一个db迁移EF添加4个FK而不是2个时 材料清单:C# EF代码首先添加它';在我定义的FK旁边有自己的FK,c#,asp.net-mvc,entity-framework,asp.net-mvc-5,C#,Asp.net Mvc,Entity Framework,Asp.net Mvc 5,我有两个类,从Identity 2.0扩展而来的applicationuser和我自己的对象。我已经定义了我自己的外键,但是当一个db迁移EF添加4个FK而不是2个时 材料清单: public class MaterialsList { [Key] public Guid Guid { get; set; } public Customer Client { get; set; } public MaterialsListStatus Status { get;
public class MaterialsList
{
[Key]
public Guid Guid { get; set; }
public Customer Client { get; set; }
public MaterialsListStatus Status { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime Date { get; set; }
public String CreatedByGuid { get; set; }
[ForeignKey("CreatedByGuid")]
public virtual ApplicationUser CreatedBy { get; set; }
public String AssignedToGuid { get; set; }
[ForeignKey("AssignedToGuid")]
public virtual ApplicationUser AssignedTo { get; set; }
public virtual ICollection<MaterialsRow> Rows { get; set; }
}
有没有人有解决这个问题的方法,最好是使用数据注释?只有在没有其他选项的情况下,我才尝试使用Fluent api。您可以通过在每个列表上添加
InverseProperty
注释来解决此问题,如前所述。本质上,Entity Framework无法自动确定ApplicationUser
适用于
public class ApplicationUser : IdentityUser
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[InverseProperty("CreatedBy")]
public virtual ICollection<MaterialsList> CreatedMaterialsLists { get; set; }
[InverseProperty("AssignedTo")]
public virtual ICollection<MaterialsList> AssignedMaterialsLists { get; set; }
}
公共类应用程序用户:IdentityUser
{
[必需]
公共字符串名{get;set;}
[必需]
公共字符串LastName{get;set;}
[反向属性(“CreatedBy”)]
公共虚拟ICollection CreatedMaterialsLists{get;set;}
[反向财产(“转让给”)]
公共虚拟ICollection AssignedMaterialsLists{get;set;}
}
public override void Up()
{
CreateTable(
"dbo.MaterialsLists",
c => new
{
Guid = c.Guid(nullable: false),
Status = c.Int(nullable: false),
Date = c.DateTime(nullable: false),
CreatedByGuid = c.String(maxLength: 128),
AssignedToGuid = c.String(maxLength: 128),
ApplicationUser_Id = c.String(maxLength: 128), // I Want to use the 2 properties above instead of these
ApplicationUser_Id1 = c.String(maxLength: 128),
Client_CustomerGuid = c.Guid(),
})
.PrimaryKey(t => t.Guid)
.ForeignKey("dbo.AspNetUsers", t => t.ApplicationUser_Id)
.ForeignKey("dbo.AspNetUsers", t => t.ApplicationUser_Id1)
.ForeignKey("dbo.AspNetUsers", t => t.AssignedToGuid)
.ForeignKey("dbo.Customers", t => t.Client_CustomerGuid)
.ForeignKey("dbo.AspNetUsers", t => t.CreatedByGuid)
.Index(t => t.CreatedByGuid)
.Index(t => t.AssignedToGuid)
.Index(t => t.ApplicationUser_Id)
.Index(t => t.ApplicationUser_Id1)
.Index(t => t.Client_CustomerGuid);
CreateTable(
"dbo.MaterialsRows",
c => new
{
Guid = c.Guid(nullable: false),
Quantity = c.Int(nullable: false),
Product_ProductGuid = c.Guid(),
MaterialsList_Guid = c.Guid(),
})
.PrimaryKey(t => t.Guid)
.ForeignKey("dbo.Products", t => t.Product_ProductGuid)
.ForeignKey("dbo.MaterialsLists", t => t.MaterialsList_Guid)
.Index(t => t.Product_ProductGuid)
.Index(t => t.MaterialsList_Guid);
}
public class ApplicationUser : IdentityUser
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[InverseProperty("CreatedBy")]
public virtual ICollection<MaterialsList> CreatedMaterialsLists { get; set; }
[InverseProperty("AssignedTo")]
public virtual ICollection<MaterialsList> AssignedMaterialsLists { get; set; }
}