Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架“;“应该无法访问的代码”;论农业包含表达_C#_Entity Framework_Lambda_Entity Framework Core_Repository Pattern - Fatal编程技术网

C# 实体框架“;“应该无法访问的代码”;论农业包含表达

C# 实体框架“;“应该无法访问的代码”;论农业包含表达,c#,entity-framework,lambda,entity-framework-core,repository-pattern,C#,Entity Framework,Lambda,Entity Framework Core,Repository Pattern,首先 // Second attempts if (includes.Length > 0) { IQueryable<TEntity> set = includes .Aggregate<Expression<Func<TEntity, object>>, IQueryable<TEntity>> (dbContext.Set<TEntity>(), (current, expre

首先

// Second attempts
if (includes.Length > 0)
{
    IQueryable<TEntity> set = includes
       .Aggregate<Expression<Func<TEntity, object>>, IQueryable<TEntity>>
       (dbContext.Set<TEntity>(), (current, expression) => current.Include(expression));

    // Here we also get "Code supposed to be unreachable"
    return await set.SingleOrDefaultAsync(s => s.Id == id).ConfigureAwait(false);
}
我已经创建了这个,只需要F5就可以命中这个错误,所以您应该可以很容易地尝试一下。这个问题中的所有链接都指向该回购协议

代码流

在我的控制器中,我希望赋予前端开发人员权力,使其能够包含所需的关系

// The included tables I want to control from my controller
Expression<Func<CompanyDto, object>>[] includes = { x => x.Employees, x => x.Cars };

var companyDto2 = await service.GetByIdAsync(1, includes).ConfigureAwait(false);
//要从控制器控制的包含表
表达式[]包括={x=>x.Employees,x=>x.Cars};
var companyDto2=await service.GetByIdAsync(1,包括).ConfigureAwait(false);
然后在我的服务层中,将它们发送到存储库

var entityIncludes = mapper.Map<Expression<Func<Entity, object>>[]>(includes);

var result = await repository.GetByIdAsync(id, entityIncludes).ConfigureAwait(false);
var entityIncludes=mapper.Map(includes);
var result=await repository.GetByIdAsync(id,entityIncludes).ConfigureAwait(false);
错误

当我在中运行include表达式时,我得到以下错误

“应该无法访问的代码”

这里有两个我尝试过的抛出这个错误的例子

第一次尝试

这是我的企图

var queryableResultWithIncludes=includes
.Aggregate(dbContext.Set().AsQueryable(),
(当前,包括)=>当前。包括(包括));
//使用规范的条件表达式返回查询结果
var result=queryableResultWithIncludes.AsEnumerable();
//这里我们得到了“应该无法访问的代码”
var neverHappens=result.ToList();
第二次尝试

// Second attempts
if (includes.Length > 0)
{
    IQueryable<TEntity> set = includes
       .Aggregate<Expression<Func<TEntity, object>>, IQueryable<TEntity>>
       (dbContext.Set<TEntity>(), (current, expression) => current.Include(expression));

    // Here we also get "Code supposed to be unreachable"
    return await set.SingleOrDefaultAsync(s => s.Id == id).ConfigureAwait(false);
}
//第二次尝试
如果(包括长度>0)
{
IQueryable集合=包含
总数的
(dbContext.Set(),(当前,表达式)=>current.Include(表达式));
//这里我们还得到了“应该无法访问的代码”
返回wait set.SingleOrDefaultAsync(s=>s.Id==Id).configureWait(false);
}
摘要


我错过了什么?我在做一些反模式的事情吗?我需要一些EF专家告诉我:-)

正如我所怀疑的,这个问题与EF没有任何共同之处,但是AutoMapper表达式翻译在这里生成的表达式无效:

var entityIncludes = mapper.Map<Expression<Func<Entity, object>>[]>(includes);
应该是

services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
// Auto Mapper Configurations
AutoMapper.Mapper.Initialize(cfg =>
{
    cfg.DisableConstructorMapping();
    cfg.AddExpressionMapping();
    cfg.AddProfile<CompanyProfile>();
});
services.AddAutoMapper(cfg =>
{
    cfg.DisableConstructorMapping();
    cfg.AddExpressionMapping();    
}, AppDomain.CurrentDomain.GetAssemblies());