相关疑难解决方法(0)

将通用预先加载方法从EF6转换为EF Core

对于EF6,我在我的通用存储库中有一个方法,我将其暴露给所有服务层,以便根据需要使用任何嵌套属性从数据库中检索实体:

public IQueryable<T> OldMethod(params Expression<Func<T, object>>[] includeProperties)
{
    var queryable = set.AsQueryable();

    return includeProperties.Aggregate(queryable, (current, includeProperty) => current.Include(includeProperty));
}
Run Code Online (Sandbox Code Playgroud)

这样,我可以通过以下方式使用该方法:

var data = repo.OldMethod(x => x.Papers, => x.People.Select(y => y.Addresses)).ToList();
Run Code Online (Sandbox Code Playgroud)

在EF6中,这将加载每个人的Papers导航属性,People导航属性和Addresses导航属性.正如预期的那样,这会在EFCore中引发异常.由于在EFCore中切换到Include - > ThenInclude方法,我不太清楚如何在我的服务层轻松复制它,我不想要任何有关EntityFramework的信息.

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

6
推荐指数
1
解决办法
324
查看次数