【问题标题】:AutoMapper Giving Error, While Mapping A Table That Contains A List. C#AutoMapper 在映射包含列表的表时出现错误。 C#
【发布时间】:2021-09-21 12:01:02
【问题描述】:

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 开发一个后端。

我的表类是这样的:

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 中删除列表时,不会发生错误,但我也没有想要的结果。
可能是什么问题?我找不到答案。

【问题讨论】:

    标签: c# .net entity-framework entity-framework-core automapper


    【解决方案1】:

    在该来源中,您可以找到单个查询和拆分查询之间的区别: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();
    

    【讨论】:

    • 你不需要IncludeProjectTo
    • 从异常中可以明显看出。 EF Core 不支持通过Select 自定义投影的Split Query。这是 AutoMapper 在您调用 ProjectTo 时生成的。
    • 请不要看不起别人,并尽力帮助@SvyatoslavDanyliv。错误是模棱两可的,我没有在代码中的任何地方使用SelectSplit Query,我什至不知道拆分查询存在。你能分享你所说的来源吗? EF Core do not support Split QueryAutoMapper generates Select call
    • 我知道 AutoMapper 的 ProjectTo 是如何工作的。没有讨论的余地,它动态生成Select。检查documentation - ... to emit a select
    • 在 EF Core 6 中看起来是 fixed
    猜你喜欢
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 2011-08-01
    相关资源
    最近更新 更多