0
public class MyEntity
{
public int MyEntityID { get; set; }
public int Foo { get; set; }
public ICollection<MyEntityDetail> MyEntityDetails { get; set; }
}
public class MyEntityDetail
{
[Key, Column(Order=0)]
public int PK { get; set; } // this will be the MyEntityID from MyEntity
[Key, Column(Order = 1)]
public int OtherPK { get; set; } // this will be manually set
public string Bar { get; set; }
}
public class MyEntityContext : DbContext
{
public DbSet<MyEntity> MyEntities { get; set; }
public DbSet<MyEntityDetail> MyEntityDetails { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
我覺得上面的代碼解釋了什麼即時試圖實現與實體框架代碼第4.2 注意到MyEntityDetail犯規包含導航屬性myEntity所。我如何將MyEntityID從MyEntity關聯到MyEntityDetail的PK?如果我不得不我可以添加MyEntity導航屬性MyEntityDetail類,但我不想使用額外的內存我從來沒有訪問的屬性。並且這種類型的實體將在我的項目中使用100次以上。謝謝一來沒有導航屬性一對多的關係
ive將MyEntity屬性添加到MyEntityDetail類並將此代碼添加到OnModelCreating modelBuilder.Entity()。HasRequired(d => d.MyEntity).WithMany(d => d.MyEntityDetails).HasForeignKey(d => d的.pk);現在它保存了相關的詳細記錄,但是當它讀取時,MyEntityDetails始終爲空。順便提一句,有複合鍵,所以PK不需要是唯一的,所以重複的條目不會是問題,只要OtherPK確實有所作爲 –
TakeMeAsAGuest