我有聯盟和automapper投影的問題。Automapper投影和聯合
我有兩個實體:
public class Entity
{
public DateTime ActionDate { get; set; }
public int SomeProp { get; set; }
}
public class ExtendedEntity
{
public DateTime ActionDate { get; set; }
public int SomeProp { get; set; }
public int SomeOtherProp { get; set; }
}
和投影:
public class EntityProjection
{
public DateTime ActionDate { get; set; }
public int SomeProp { get; set; }
public int SomeOtherProp { get; set; }
public string Source { get; set; }
}
我的實體映射到一個投影,實體沒有SomeOtherProp,所以我設置0到它:
public class EntityProfile : Profile
{
protected override void Configure()
{
CreateMap<ExtendedEntity, EntityProjection>()
.ForMember(dest => dest.Source, opt => opt.MapFrom(src => "ext entity"));
CreateMap<Entity, EntityProjection>()
.ForMember(dest => dest.SomeOtherProp, opt => opt.MapFrom(src => 0))
.ForMember(dest => dest.Source, opt => opt.MapFrom(src => "entity"));
}
}
當我嘗試使用下一個代碼我得到錯誤:
var entities = context.Set<Entity>()
.Project().To<EntityProjection>();
var extEntities = context.Set<ExtendedEntity>()
.Project().To<EntityProjection>();
var result = entities.Union(extEntities).OrderBy(p => p.ActionDate).ToList();
錯誤文本:類型'UserQuery + EntityProjection'出現在單個LINQ to Entities查詢中的兩個結構不兼容的初始化中。一個類型可以是...
這意味着投影中的屬性必須以相同的順序初始化,我如何通過automapper設置投影屬性的初始化順序?