我可以强制Automapper按一定顺序初始化属性吗? [英] Can I force Automapper to initialise properties in a certain order?

查看:110
本文介绍了我可以强制Automapper按一定顺序初始化属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Automapper和Queryable Extensions将某些(相似)模型投影到同一DTO中,以便对它们进行分类和排序以便在DB服务器上进行分页(通过Entity Framework),而不会返回数据直到分页.

I'm using Automapper and the Queryable Extensions to project some (similar) models into the same DTO in order to concatenate and sort them for paging on the DB server (via Entity Framework) without returning the data until it is paged.

        var tasksType1 = context.TasksType1.Project().To<MyDto>();
        var tasksType2 = context.TasksType2.Project().To<MyDto>();
        var allTasks = tasksType1.Concat(tasksType2);
        return allTasks.ToMyPagedList()

这很好用,除了现在我的模型变得稍微复杂一点之外,Automapper似乎每次都以略有不同的顺序初始化投影,这会导致错误:

This works well, except that now my models have got slightly more complex, Automapper seems to initialise the projection in a slightly different order each time which results in the error:

类型'AngularJSAuthentication.API.Dtos.ActivityDashboard.ActivityDashboardDto2'出现在单个LINQ to Entities查询中的两个结构不兼容的初始化中.可以在同一查询的两个地方初始化一个类型,但前提是两个地方都设置了相同的属性,并且这些属性以相同的顺序设置.

The type 'AngularJSAuthentication.API.Dtos.ActivityDashboard.ActivityDashboardDto2' appears in two structurally incompatible initializations within a single LINQ to Entities query. A type can be initialized in two places in the same query, but only if the same properties are set in both places and those properties are set in the same order.

是否有任何方法可以强制Automapper以可控制的方式初始化投影?我已经设置了映射,以使用.ForMember和.MapFrom来定义两种类型的每个属性,这两种类型的顺序完全相同,但是即使这样做似乎也不行,并且查看所创建的SQL,似乎还是与初始模型中的属性顺序有关.

Is there any way of forcing Automapper to initialise the projection in a controllable manner? I have already set up the maps to define every property using .ForMember and .MapFrom in exactly the same order for both types but even that doesn't seem to do it, and looking at the SQL which is created, it doesn't seem to have anything to do with the property order in the initial model either.

感谢您收到任何建议!

推荐答案

我将修改此查询以将两者合并在一起,然后然后应用投影:

I'd modify this query to concat the two together, then apply projections:

    var tasksType1 = context.TasksType1;
    var tasksType2 = context.TasksType2;
    var allTasks = tasksType1.Concat(tasksType2);
    return allTasks.ProjectTo<MyDto>().ToMyPagedList()

LINQ提供程序可能无法处理投影的复杂性,而不能进行级联,但可能能够处理相反的情况.

The LINQ provider likely can't handle the complexity of a projection then a concatenation, but might be able to handle the reverse.

通常,在AutoMapper中,我尝试使投影成为该过程的最后一步.

In general with AutoMapper I try to have projection be the very last step in the process.

这篇关于我可以强制Automapper按一定顺序初始化属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆