クラス Box があるとします。
public class Box
{
[Required]
[Key]
int BoxId { get; set; }
string BoxName { get; set; }
}
ボックスを他のボックスに追加できるようにしたい - ボックスは多くのボックスを持つことも、1 つのボックスに属することもできますが、どちらも必要ありません。
私は自分のプロジェクトで次のようにモデル化しようとしました:
public class Box
{
[Required]
[Key, ForeignKey("ParentBox")]
int BoxId { get; set; }
string BoxName { get; set; }
int ParentBoxId { get; set; }
Box ParentBox { get; set; }
List<Box> Boxes {get; set;}
}
ただし、この質問で対処する次のエラーが表示されます。
「App.Core.Set_ParentSet」関係のプリンシパル エンドを特定できません。追加された複数のエンティティが同じ主キーを持つ場合があります。
ForeignKey 属性を削除するとデータベースを構築できますが、カスケード削除が機能しません。
ボックスがボックスに属しているかどうか、またはボックスを持っているかどうかは、アプリケーションで常に変化するため、ChildBox または ParentBox 用に別のクラスを作成したくありません。
これをEFでモデル化する適切な方法は何ですか?