问题描述
我的课程配置如下:
class Item {
Guid ParentId { get; set; }
// ...
}
class ClassWithItemCollectionA {
Guid Id { get; set; }
ICollection<Item> Items { get; set; }
// ...
}
class ClassWithItemCollectionB {
Guid Id { get; set; }
ICollection<Item> Items { get; set; }
// ...
}
我想填充ClassWithItemCollectionA.Items
和ClassWithItemCollectionB.Items
导航属性,将实体与Item.ParentId -> ClassWithItemCollectionA.Id
和Item.ParentId -> ClassWithItemCollectionB.Id
连接起来。
我尝试了不同的方法,但是最终却获得了令人讨厌的结果。
- 如果让EF Core创建导航属性,则会为我添加了
Item
集合的每种实体类型创建一个额外的列。 - 如果我强制EF Core使用ParentId作为外键,它将创建没有额外列的数据库架构,但是会创建额外的约束,使我无法在
Item
表中插入任何内容。 - 我可以将
Items
集合设为未映射,并进行额外的查询以针对每个ClassWithItemCollectionX
填充它,从而导致N + 1问题。
有什么方法可以定义这样的基类:
class BaseClassWithItemCollection {
Guid Id { get; set; }
ICollection<Item> Items { get; set; }
}
然后指定如何检索Items
集合,或通过其他任何方式达到我所寻找的目的?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)