Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.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# EF核心/自动映射投影导致NullReferenceError_C#_Entity Framework Core_Automapper_Ef Core 5.0 - Fatal编程技术网

C# EF核心/自动映射投影导致NullReferenceError

C# EF核心/自动映射投影导致NullReferenceError,c#,entity-framework-core,automapper,ef-core-5.0,C#,Entity Framework Core,Automapper,Ef Core 5.0,我正在使用AutoMPper和EF core 5,在使用投影时,在尝试获取子列表的计数时遇到了一个问题。如果我尝试获取子对象列表,效果良好,我的视图模型如下: 公共类CurrentRunningBatchView { 公共Guid Id{get;set;} public int BatchNo{get;set;} 公共int属性搜索{get;set;} 公共int属性Arched{get;set;} 公共分类类型枚举分类类型{get;set;} 公共日期时间起始日期{get;set;} 公共日期

我正在使用AutoMPper和EF core 5,在使用投影时,在尝试获取子列表的计数时遇到了一个问题。如果我尝试获取子对象列表,效果良好,我的视图模型如下:

公共类CurrentRunningBatchView
{
公共Guid Id{get;set;}
public int BatchNo{get;set;}
公共int属性搜索{get;set;}
公共int属性Arched{get;set;}
公共分类类型枚举分类类型{get;set;}
公共日期时间起始日期{get;set;}
公共日期时间估计完成时间{get;set;}
公共列表分类{get;set;}
}
我的地图在这里:

公共类CurrentRunningBatchViewProfile:Profile
{
public CurrentRunningBatchViewProfile()
{
CreateMap()
//.ConvertUsing()
.ForMember(c=>c.Classifications,o=>o.MapFrom(s=>s.Classifications.Take(10)))
//.FormMember(c=>c.ClassificationType,o=>o.MapFrom(s=>ClassificationTypeEnum.FromValue)
.FormMember(s=>s.PropertiesArching,d=>d.MapFrom(s=>s.Classifications.Count))
//.FormMember(s=>s.PropertiesArched,d=>d.MapFrom(s=>s.Classifications.Count(c=>c.ClassificationResults.Any()))
//.AfterMap();
;
}
}
分类属性工作正常,只是在那里证明它工作正常,如果我注释掉propertiessearch行,那么它工作正常。 调试代码中的查询表达式看起来很好:

DbSet()
.Where(b=>b.EndDate==null&&b.StartDate!=null)
.包括(“分类”)
.AsNoTracking()
.选择(dtoClassificationBatch=>new CurrentRunningBatchView{
BatchNo=dtoClassificationBatch.BatchNo,
分类=dtoClassificationBatch.Classifications
.Take(10)
.ToList(),
ClassificationType=dtoClassificationBatch.ClassificationType,
Id=dtoClassificationBatch.Id,
PropertiesArching=DTocClassificationBatch.Classifications.Count,
StartDate=DTocClassificationBatch.StartDate??01/01/0001 00:00:00
}
)
.FirstOrDefault()
我得到的错误是:

Object reference not set to an instance of an object.
   at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ApplyCollectionJoin(Int32 collectionIndex, Int32 collectionId, Expression innerShaper, INavigationBase navigation, Type elementType, Boolean splitQuery)

我猜问题在于automapper而不是EF core?但是在编译查询表达式时,我似乎无法获得任何关于它所做工作的数据,非常感谢任何帮助

我在映射中找到了问题的答案,而不是调用.Count,我需要调用.Count(),我认为这是一个EF问题,因为我去掉了所有AutoMapper代码并直接在DBContext上进行了选择,然后将工作代码转移到自动映射中

如果在.Count之前添加一个.ToList()会有帮助吗?看起来像EFC bug。