2015-08-15 45 views
0

我有2個類,我的應用程序用戶,從身份2.0擴展,和我自己的對象。腳手架數據庫遷移EF當我無論如何定義自己的外鍵增加了4 FK的,而不是2EF代碼首先添加它自己的FK旁邊的我定義

MaterialsList:

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; } 
} 

ApplicationUser:

public class ApplicationUser : IdentityUser 
{ 
    [Required] 
    public string FirstName { get; set; } 

    [Required] 
    public string LastName { get; set; } 

    public virtual ICollection<MaterialsList> CreatedMaterialsLists { get; set; } 
    public virtual ICollection<MaterialsList> 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); 

    } 

有沒有人有解決方案,最好用數據註釋S'我試圖只在沒有其他選項時才使用Fluent API。

回答

2

您可以通過在每個列表中添加InverseProperty註釋來解決此問題,如here所述。實質上,實體框架無法自動確定適用於哪個外鍵每個列表適用於。

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; } 
}