C# IEnumerable到EntityCollection使用嵌套映射与automapper进行映射失败
从2.2.0开始,我对nasted映射有一个问题 我需要映射两个模型:EntityObject模型(由EF从DB自动生成)和简单数据模型。 EntityObject模型包含另一个EntityObject模型类型的EntityCollection属性,数据模型包含另一个数据模型类型的IEnumerable:还应映射这些字段。例如:C# IEnumerable到EntityCollection使用嵌套映射与automapper进行映射失败,c#,asp.net-mvc,entity-framework,model,automapper,C#,Asp.net Mvc,Entity Framework,Model,Automapper,从2.2.0开始,我对nasted映射有一个问题 我需要映射两个模型:EntityObject模型(由EF从DB自动生成)和简单数据模型。 EntityObject模型包含另一个EntityObject模型类型的EntityCollection属性,数据模型包含另一个数据模型类型的IEnumerable:还应映射这些字段。例如: public class AnotherDataModel { //Some properties } public class DataModel
public class AnotherDataModel
{
//Some properties
}
public class DataModel
{
//Some properties
private IEnumerable<AnotherDataModel> anotherDataModel;
public IEnumerable<AnotherDataModel> AnotherDataModel
{
get { return anotherDataModel ?? (anotherDataModel = new AnotherDataModel[0]); }
set { anotherDataModel = value; }
}
}
public partial class AnotherModel : EntityObject
{
//Some properties
}
public partial class Model : EntityObject
{
//Some properties
public EntityCollection<AnotherModel> AnotherModel
{
get
{
return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<AnotherModel>(//relationship settings);
}
set
{
if ((value != null))
{
((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<AnotherModel>(//relationship settings, value);
}
}
}
}
}
公共类另一个数据模型
{
//一些性质
}
公共类数据模型
{
//一些性质
私有IEnumerable另一个数据模型;
公共IEnumerable另一个数据模型
{
获取{return anotherDataModel??(anotherDataModel=new anotherDataModel[0]);}
设置{anotherDataModel=value;}
}
}
公共部分类AnotherModel:EntityObject
{
//一些性质
}
公共部分类模型:EntityObject
{
//一些性质
公共实体集合AnotherModel
{
得到
{
返回((IEntityWithRelationships)this.RelationshipManager.GetRelatedCollection(//关系设置);
}
设置
{
如果((值!=null))
{
((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection(//关系设置,值);
}
}
}
}
}
我需要将数据模型映射到模型。两种需要的地图都存在:
Mapper.CreateMap<AnotherDataModel, AnotherModel>();
Mapper.CreateMap<DataModel, Model>();
Mapper.CreateMap();
CreateMap();
但在将数据模型映射到模型时,我在将另一个数据模型映射到另一个Thermodel属性时遇到了一个错误:
EntityCollection已初始化。InitializeRelatedCollection方法只应在反序列化对象图期间调用以初始化新EntityCollection。
它在automapper 2.0上运行良好。我尝试将UseDestinationValue用于此字段,但结果相同
在其他地方,我在映射IEnumerable时也遇到了很多“集合大小固定”的错误,尽管这样的问题已经解决,但我已经用自定义解析器解决了:
public class EnumerableResolver<TCollectionOfInputType, TCollectionOfOutputType> :
ValueResolver<IEnumerable<TCollectionOfInputType>, IEnumerable<TCollectionOfOutputType>>
{
public IEnumerable<TCollectionOfOutputType> Resolve(IEnumerable<TCollectionOfInputType> source)
{
return this.ResolveCore(source);
}
protected override IEnumerable<TCollectionOfOutputType> ResolveCore(IEnumerable<TCollectionOfInputType> source)
{
return source == null
? null
: source.Select(Mapper.Map<TCollectionOfInputType, TCollectionOfOutputType>);
}
公共类枚举器解决方案:
值分解器
{
公共IEnumerable解析(IEnumerable源)
{
返回此.ResolveCore(源);
}
受保护的覆盖IEnumerable ResolveCore(IEnumerable源)
{
返回源==null
无效的
:source.Select(Mapper.Map);
}
在这种情况下它不起作用。另外,当IEnumerable与EntityCollection不嵌套时,将它们映射到EntityCollection也可以正常工作:
Mapper.CreateMap<IEnumerable<AnotherDataModel>, EntityCollection<AnotherModel>>();
Mapper.CreateMap();
任何帮助都将不胜感激。试试这个
class Program
{
static void Main(string[] args)
{
Mapper.CreateMap<LocationSource, LocationDestination>();
Mapper.CreateMap<StoreSource, StoreDestination>();
var storeSource = new StoreSource
{
Name = "Worst Buy",
Locations = new EntityCollection<LocationSource> { new LocationSource { Id = 1, Address ="abc1"},new LocationSource { Id = 2, Address ="abc2"}}
};
var storeDestination = Mapper.Map<StoreSource, StoreDestination>(storeSource);
}
}
public class StoreDestination
{
public string Name { get; set; }
public IList<LocationDestination> Locations { get; set; }
}
public class LocationDestination
{
public int Id { get; set; }
public string Address { get; set; }
}
public class StoreSource
{
public string Name { get; set; }
public EntityCollection<LocationSource> Locations { get; set; }
}
public class LocationSource
{
public int Id { get; set; }
public string Address { get; set; }
}
类程序
{
静态void Main(字符串[]参数)
{
CreateMap();
CreateMap();
var storeSource=新的storeSource
{
Name=“最差购买”,
Locations=new EntityCollection{new LocationSource{Id=1,Address=“abc1”},new LocationSource{Id=2,Address=“abc2”}
};
var storeDestination=Mapper.Map(storeSource);
}
}
公共类目的地
{
公共字符串名称{get;set;}
公共IList位置{get;set;}
}
公共类位置目的地
{
公共int Id{get;set;}
公共字符串地址{get;set;}
}
公共类存储源
{
公共字符串名称{get;set;}
公共EntityCollection位置{get;set;}
}
公共类位置源
{
公共int Id{get;set;}
公共字符串地址{get;set;}
}
这是automapper中的一个bug,现已在最新版本中修复
github上的问题及其详细信息以防万一:请压缩代码块以减少滚动。此示例有效,因此我认为在setter中使用InitializeRelatedCollection是导致问题的主要条件。此集合可能已初始化两次。无论如何,我无法避免使用它,因为它只是由生成的实体数据模型数据库中的实体框架。