Entity Framework 4.1では、共有外部キー(entityの値オブジェクト)のマッピング提案?
-
07-09-2020 - |
質問
プロジェクトエンティティとRFIエンティティを持っています。プロジェクトエンティティにはチームメンバーのリストが含まれています。プロジェクトはRFIエンティティのナビゲーションプロパティです。RFIエンティティでは、受信者がいます。このIDはTeamMembersコレクションの人物を表します。そのため、Webページでは、受信者のドロップダウンボックスがあります。リストにはプロジェクトのすべてのチームメンバーが含まれています。ユーザーはそのリストから連絡先を選択します。その連絡先のIDはrecipientidIdプロパティに保存されます。ページが再ロードされると、RecipeIntSidプロパティの値からドロップダウンでそのユーザーのIDを選択します。これをEF 4.1にマッピングする最善の方法は、流暢なAPIを使用していますか?
public class Project : BaseEntity
{
public string ProjectNumber { get; set; }
public string Description { get; set; }
public string CreatedBy { get; set; }
public string ModifiedBy { get; set; }
public string Currency { get; set; }
#region Navigation Properties
public Guid AddressId { get; set; }
public virtual Address Address { get; set; }
public Guid CompanyCodeId { get; set; }
public virtual CompanyCode CompanyCode { get; set; }
public virtual ICollection<Contact> TeamMembers { get; set; }
#endregion
}
public class Rfi : Document
{
public string Number { get; set; }
public string Subject { get; set; }
public string SubcontractorRfiReference { get; set; }
public string SpecificationSection { get; set; }
public RfiStatus RfiStatus { get; set; }
public Guid RecipientId { get; set; }
#region Navigation Properties
public Guid ProjectId { get; set; }
public Project Project { get; set; }
#endregion
}
. 解決
私はあなたの問題を理解しているので、Rfi
とContect
- Project
の間のマッピングは、データベースの観点から受信者の機能に関与しません。
Recipient
のRfi
またはRfis
NavigationプロパティのContact
Navigationプロパティのどちらかを必要です。EFコード最初の関係の少なくとも片側にナビゲーションプロパティが必要です。
だからあなたは次のようなものを使うことができます:
public class Rfi : Document
{
public string Number { get; set; }
public string Subject { get; set; }
public string SubcontractorRfiReference { get; set; }
public string SpecificationSection { get; set; }
public RfiStatus RfiStatus { get; set; }
#region Navigation Properties
public Guid RecipientId { get; set; }
public Contact Recipient { get; set; }
public Guid ProjectId { get; set; }
public Project Project { get; set; }
#endregion
}
.
とマップ:
modelBuilder.Entity<Rfi>()
.HasRequired(r => r.Recipient)
.WithMany()
.HasForeignKey(r => r.RecipientId);
. 所属していません StackOverflow