レポート セクションの階層ツリーを持つレポートを表す EF データ モデルがあります。各 ReportSection エンティティには、0 個以上の子 ReportSection のコレクションが含まれます。各 Report エンティティには、ReportSection のツリーのルートとして機能する単一の ReportSection エンティティが含まれます。
次のナビゲーション プロパティを持つデータ モデル:
public class Report
{
// Primary key
public int Id { get; set; }
// A Report has one root ReportSection
[ForeignKey("RootReportSection")]
public int ReportSectionId { get; set; }
public virtual ReportSection RootReportSection { get; set; }
}
public class ReportSection
{
// Primary key
public int Id { get; set; }
// Optional self-reference to the parent ReportSection
[ForeignKey("ParentReportSection")]
public int? ParentReportSectionId { get; set; }
public virtual ReportSection ParentReportSection { get; set; }
// Optional foreign key to the parent Report
[ForeignKey("ParentReport")]
public int? ReportId { get; set; }
public virtual Report ParentReport { get; set; }
// Child ReportSections contained in this ReportSection
public virtual ICollection<ReportSection> ReportSections { get; set; }
}
Report エンティティからナビゲーション claptrapを省略するReportSectionId
と、すべて正常に動作します。RootReportSection
ただし、上記のコードのように、移行を追加しようとするとエラーが発生します。
Because the Dependent Role properties are not the key properties,
the upper bound of the multiplicity of the Dependent Role must be '*'.
少し掘り下げた後、EF が ReportSections エンティティの主キーを Report エンティティの外部キーとして使用することを望んでいることを理解しました。しかし、私のシナリオでは、ReportSection エンティティの階層ツリーの最上位の ReportSection のみが Report エンティティとの関係に参加します。残りの ReportSection エンティティは相互に関連しており、それらの主キーは Report の主キーから独立しています。
これを機能させる方法はありますか?具体的には、Report エンティティが最上位の ReportSection エンティティを「含む」方法はありますか?