从2.2.0开始,我遇到了带有映射的问题。
我需要映射两个模型:EntityObject模型(由DB自动生成)和一个简单的数据模型。 EntityObject模型包含另一个EntityObject模型类型的EntityCollection属性,Data Model包含另一个Data Model类型的IEnumerable:这些字段也应该被映射。举个例子:
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);
}
}
}
}
}
我需要将DataModel映射到Model。存在两个所需的地图:
Mapper.CreateMap<AnotherDataModel, AnotherModel>();
Mapper.CreateMap<DataModel, Model>();
但是,在将DataModel映射到Model i时,将AnotherDataModel映射到AnotherModel属性时出错:
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映射到EntityCollection而它们没有嵌套时 - 工作正常:
Mapper.CreateMap<IEnumerable<AnotherDataModel>, EntityCollection<AnotherModel>>();
任何帮助都将不胜感激。
答案 0 :(得分:0)
试试这个,
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; }
}
答案 1 :(得分:0)
这是automapper中的一个错误,现在已在最新版本中修复。
github上的问题以及以下内容的详细信息:https://github.com/AutoMapper/AutoMapper/issues/425