.thenInclude用于Entity Framework Core 2中的子实体 [英] .ThenInclude for sub entity in Entity Framework Core 2

查看:116
本文介绍了.thenInclude用于Entity Framework Core 2中的子实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前(使用.net 4.5.2和EF 6时)。我有一个通用的 Get 方法,该方法接受了以下包含内容;

Previously (when using .net 4.5.2 and EF 6). I have had a generic Get method that accepted a number of includes as follows;

public abstract class DataContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>, IDataContext
{
    public DataContext(DbContextOptions options)
    : base(options)
    {
    }

    // reduced for brevity

    public T Get<T>(int id, params Expression<Func<T, object>>[] includes) where T : class, IEntity
    {
        return this.Set<T>().Include(includes).FirstOrDefault(x => x.Id == id);
    }

我会举个例子;

context.Get<Job>(id, 
    x => x.Equipment,
    x => x.Equipment.Select(y => y.Type));

包括 Job.Equipment 以及 Job.Equipment.Type

但是,当我将其移植到asp.net core 2上时。我尝试了相同的通用方法,但是如果尝试包含一个子实体,则会出现以下错误;

However, when I have ported this over to asp.net core 2. I have tried the same generic approach, but if I try to include a sub-entity I get the following error;


属性表达式' x => {从x。设备中的设备y中选择[y]。类型}'无效。该表达式应表示属性访问权限: t => t.MyProperty。有关包括相关数据的更多信息,请参见 http://go.microsoft.com/fwlink/ ?LinkID = 746393

有人可以建议我如何解决此问题,以将子实体包括在我的通用<$中使用Entity Framework Core 2的c $ c> Get< T> 方法?

Can anyone suggest how I can work around this to include sub entities in my Generic Get<T> method with Entity Framework Core 2?

更新

从查看文档开始,还有一个附加的include方法

From looking at the documents there is an additional include method

include(string navigationPropertyPath)

我添加了以下方法;

    public T Get<T>(int id, string[] includes) where T : class, IEntity
    {
        var result = this.Set<T>().AsQueryable();

        foreach(var include in includes)
        {
            result = result.Include(include);
        }

        return result.FirstOrDefault(x => x.Id == id);
    }

尽管我对这里的效率不抱有信心,但哪一项有效? p>

Which does work, although I am not convinced on the efficiency here?

推荐答案

EF核心 Include / ThenInclude 模式不能用EF6中的 Expression< Func< T,object>>> [] 表示。

EF Core Include / ThenInclude pattern cannot be represented by Expression<Func<T, object>>[] like in EF6.

查看EF Core扩展之一的源代码- Microsoft.EntityFrameworkCore.UnitOfWork ,它声称是

Looking at the source code of one of the EF Core extensions - Microsoft.EntityFrameworkCore.UnitOfWork, which claims to be


A Microsoft.EntityFrameworkCore的插件,用于支持存储库,工作单元模式以及支持分布式事务的多个数据库。

A plugin for Microsoft.EntityFrameworkCore to support repository, unit of work patterns, and multiple database with distributed transaction supported.

看起来像预期的模式for包括应该基于 Func< IQueryable< T,IIncludableQueryable< T,object>>

looks like the intended pattern for includes should be based on Func<IQueryable<T>, IIncludableQueryable<T, object>>:

public T Get<T>(int id, Func<IQueryable<T>, IIncludableQueryable<T, object>> include = null) where T : class, IEntity
{
    var result = this.Set<T>().AsQueryable();

    if (include != null)
        result = include(result);

    return result.FirstOrDefault(x => x.Id == id);
}

缺点是它在调用方上增加了EF Core依赖项,并且需要使用Microsoft.EntityFrameworkCore; 。在您的情况下这不是必需的,因为您要扩展 DbContext

The drawback is that it adds EF Core dependency on the caller and requires using Microsoft.EntityFrameworkCore;. Which in your case is not essential since you are extending the DbContext.

示例中的用法是:

context.Get<Job>(id, q => q
    .Include(x => x.Equipment)
        .ThenInclude(y => y.Type));

这篇关于.thenInclude用于Entity Framework Core 2中的子实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆