2017-10-06 199 views
0

當我將兩個實體鏈接到具有相同遺產導航屬性的實體時,我無法生成我的分數.. 我看到我可以複製屬性「房子」並指定每個遺留類型的InverseProperty,但我不會集中財產。 (當然,我有更多的在財產相關的代碼)實體框架繼承InverseProperty

public abstract class DbEntity 
{ 
    [Key] 
    public int Id { get; set; } 
} 

[Table("Houses")] 
public class House : DbEntity 
{ 
    public string Color { get; set; } 

    [InverseProperty("House")] 
    public virtual ICollection<Garden> Gardens { get; set; } 

    [InverseProperty("House")] 
    public virtual ICollection<Room> Rooms { get; set; } 
} 


public abstract class HousePart : DbEntity 
{ 
    public string Color { get; set; } 

    public int HouseId { get; set; } 
    [ForeignKey("HouseId")] 
    public virtual House House { get; set; } 
} 
[Table("Gardens")] 
public class Garden : HousePart {} 
[Table("Rooms")] 
public class Room : HousePart {} 

樓:FromRole:NavigationProperty '房子' 是無效的。在AssociationType'House_Rooms'中輸入FromRole'House_Rooms_Target'的'Room'必須與聲明此NavigationProperty的類型'Garden'完全匹配。

謝謝,如果你有一個很好的方法來集中實施財產。

回答

0

那麼這是對那樣的工作:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<Garden>() 
     .HasRequired(c => c.House) 
     .WithMany(d => d.Gardens) 
     .HasForeignKey(d => d.HouseId); 

     modelBuilder.Entity<Room>() 
      .HasRequired(c => c.House) 
      .WithMany(d => d.Rooms) 
      .HasForeignKey(d => d.HouseId); 

     base.OnModelCreating(modelBuilder); 
    }