当我将两个实体链接到一个具有相同herited导航属性的实体时,我无法生成我的数据库.. 我看到我可以复制该物业" House"并指定每种类型的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 {}
House:FromRole:NavigationProperty&#39; House&#39;无效。键入&#39;房间&#39; FromRole&#39; House_Rooms_Target&#39;在AssociationType&#39; House_Rooms&#39;必须与花园&#39;花园相匹配声明此NavigationProperty的位置。
谢谢你有一个很好的方法来集中实现属性。
答案 0 :(得分: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);
}