小编Mar*_*koo的帖子

使用 EF Core 2.1 继承进行过滤

当使用继承的对象时,我试图找到一种方法来过滤 EF Core 2.1 中的结果。

我有一个基本模型和几个继承的类(但我只包含了一个):

public class Like {
    public int Id { get; set; }
    public LikeType LikeType { get; set; }
}

public class DocumentLike : Like {
    [ForeignKey(nameof(Document))]
    public int DocumentId { get; set; }
    public virtual Document Document { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

LikeType是一个枚举,它被定义为 dbcontext 中的鉴别器。每个Document都有一个布尔属性.IsCurrent。

要从数据库中获取所有项目,我正在使用如下查询:

IQueryable<Like> query = _context.Set<Like>()
    .Include(x => x.Owner)
    .Include(x => (x as DocumentLike).Document.DocumentType)
    .Include(x => (x as ProductLike).Product)
    .Include(x => (x as …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework-core .net-core ef-core-2.1

4
推荐指数
1
解决办法
3871
查看次数