AutoMapper 在映射包含列表的 Table 时出错。 C#
AutoMapper Giving Error, While Mapping A Table That Contains A List. C#
TLDR - 错误是:
The query has been configured to use 'QuerySplittingBehavior.SplitQuery' and contains a collection in the 'Select' call, which could not be split into separate query. Please remove 'AsSplitQuery' if applied or add 'AsSingleQuery' to the query.
我正在使用 C# 中的 EntityFrameworkCore 开发后端。
我的table类是这样的:
public class MainTable : BasicAggregateRoot<int>
{
public MainTable()
{
this.Operations = new HashSet<OperationTable>();
}
public long? RecId { get; set; }
public int FormStatus { get; set; }
public virtual ICollection<OperationTable> Operations { get; set; }
}
public class OperationTable : BasicAggregateRoot<int>
{
public OperationTable()
{
this.Works = new HashSet<Work>(); //Not important things
this.Materials = new HashSet<Material>(); //Not important things
}
public string ServiceType { get; set; }
}
而我的 DTO 是这样的:
public class MainDto : EntityDto<int>
{
public long? RecId { get; set; }
public int FormStatus { get; set; }
public List<OperationDto> Operations { get; set; }
}
public class OperationDto
{
public string ServiceType { get; set; }
}
我是这样创建地图的:
CreateMap<MainTable, MainDto>().ReverseMap();
CreateMap<OperationTable, OperationDto>().ReverseMap();
当我通过以下方式提交映射时:
class Service{
IRepository<MainTable, int> _mainTableRepository;
Service(IRepository<MainTable, int> mainTableRepository){
_mainTableRepository = mainTableRepository;
}
List<MainDto> All()
{
var result = mainTableRepository.Include(p => p.Operations)
.ProjectTo<MainDto>(ObjectMapper.GetMapper().ConfigurationProvider) //Here is the problem.
.ToList();
return result;
}
}
我在顶部看到错误。
当我从 mainDto 中删除列表时,没有发生错误,但我也没有得到我想要的结果。
可能是什么问题?我找不到答案。
在该来源中,您可以找到单一查询和拆分查询之间的区别:https://docs.microsoft.com/en-us/ef/core/querying/single-split-queries
问题是(我猜)IRepository.Include
默认使用拆分查询。但是(我再次猜测)AutoMapper 未配置为使用拆分查询,它适用于单个查询。
我们需要像这样在映射之前更改查询类型:
var result = mainTableRepository.Include(p => p.Operations)
.AsSingleQuery() //This solved the problem
.ProjectTo<MainDto>(ObjectMapper.GetMapper().ConfigurationProvider)
.ToList();
TLDR - 错误是:
The query has been configured to use 'QuerySplittingBehavior.SplitQuery' and contains a collection in the 'Select' call, which could not be split into separate query. Please remove 'AsSplitQuery' if applied or add 'AsSingleQuery' to the query.
我正在使用 C# 中的 EntityFrameworkCore 开发后端。
我的table类是这样的:
public class MainTable : BasicAggregateRoot<int>
{
public MainTable()
{
this.Operations = new HashSet<OperationTable>();
}
public long? RecId { get; set; }
public int FormStatus { get; set; }
public virtual ICollection<OperationTable> Operations { get; set; }
}
public class OperationTable : BasicAggregateRoot<int>
{
public OperationTable()
{
this.Works = new HashSet<Work>(); //Not important things
this.Materials = new HashSet<Material>(); //Not important things
}
public string ServiceType { get; set; }
}
而我的 DTO 是这样的:
public class MainDto : EntityDto<int>
{
public long? RecId { get; set; }
public int FormStatus { get; set; }
public List<OperationDto> Operations { get; set; }
}
public class OperationDto
{
public string ServiceType { get; set; }
}
我是这样创建地图的:
CreateMap<MainTable, MainDto>().ReverseMap();
CreateMap<OperationTable, OperationDto>().ReverseMap();
当我通过以下方式提交映射时:
class Service{
IRepository<MainTable, int> _mainTableRepository;
Service(IRepository<MainTable, int> mainTableRepository){
_mainTableRepository = mainTableRepository;
}
List<MainDto> All()
{
var result = mainTableRepository.Include(p => p.Operations)
.ProjectTo<MainDto>(ObjectMapper.GetMapper().ConfigurationProvider) //Here is the problem.
.ToList();
return result;
}
}
我在顶部看到错误。
当我从 mainDto 中删除列表时,没有发生错误,但我也没有得到我想要的结果。
可能是什么问题?我找不到答案。
在该来源中,您可以找到单一查询和拆分查询之间的区别:https://docs.microsoft.com/en-us/ef/core/querying/single-split-queries
问题是(我猜)IRepository.Include
默认使用拆分查询。但是(我再次猜测)AutoMapper 未配置为使用拆分查询,它适用于单个查询。
我们需要像这样在映射之前更改查询类型:
var result = mainTableRepository.Include(p => p.Operations)
.AsSingleQuery() //This solved the problem
.ProjectTo<MainDto>(ObjectMapper.GetMapper().ConfigurationProvider)
.ToList();