【发布时间】:2021-11-16 20:49:59
【问题描述】:
如果连接表的一侧是按层次结构设置的表中的派生类,我在使用 EF 核心设置多对多连接表时遇到问题。
设置如下:
class Chore
{
Guid Id;
}
class LaundryChore : Chore
{
// PROBLEMATIC
List<Clothing> ManyClothing;
}
class FoldingChore : Chore
{
Clothing SingleClothing;
}
class Clothing
{
Guid Id;
// PROBLEMATIC
List<Chore> Chores;
}
我用鉴别器设置了 TPH,一切正常。如果 ManyClothing 字段在 Chore 类上,那么我可以这样做:
builder.Entity<Clothing>().HasMany(clothing => clothing.Chores)
.WithMany(chore => chore.ManyClothing);
这按预期工作。
但由于 ManyClothing 字段在 LaundryChore 类上,我会收到上面的 DNE 错误。
我试着换个方向:
builder.Entity<LaundryChore>().HasMany(chore => clothing.ManyClothing)
.WithMany(clothing => clothing.Chores);
我得到一个转换错误:
无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.IEnumerable”
如果我改为:
class Clothing
{
Guid Id;
List<LaundryChore> Chores;
}
那么我得到的错误是:
无法为实体类型“LaundryChore”指定过滤器表达式 ...。过滤器只能应用于根实体类型“杂项”
任何指导将不胜感激 - 谢谢!
【问题讨论】:
-
Clothing中的集合导航属性必须为List<LaundryChore>。那么过滤器错误是一个不同的问题,是由您没有显示的代码引起的。
标签: c# entity-framework-core many-to-many ef-fluent-api table-per-hierarchy