这肯定是一个特殊的问题,在发现没有人遇到同样的问题之后,我希望我能找到真正知道问题出在哪里的人。
我有,说:
public class ViewModel
{
public string TestString { get; set; }
public ChildObject Child { get; set; }
public List<Children> Children { get; set; }
}
现在,当我去映射我的 Controller 中的对象时......即
TestObject testObject = Repository.GetObject() // This is my Entity Framework object (with dynamic proxies attached to it)
Mapper.Map(ViewModel, testObject);
并且,我的映射定义为:
Mapper.CreateMap<ViewModel, TestObjectType>();
Mapper.CreateMap<ViewModelChildView, TestObjectChildType>();
一切都很好,所以我已经正确设置了我的 ViewModel 和我的表单。问题是?这是有趣的部分。
ChildObject
与我的原始对象一样,是 DynamicProxy( Entity Framework )。 List<Children>
但是,设置为普通列表(即使原始列表是 List<Children>
的 DynamicProxy)。
反过来,这意味着 Entity Framework 无法访问此列表,因为它未正确绑定(bind)到上下文。我一整天都在尝试找到一个没有运气的解决方法。有什么想法吗?
编辑
这些是我实际的对象绑定(bind):
Mapper.CreateMap<CompanyViewModel, Company>();
Mapper.CreateMap<CompanyCategoryViewModel, CompanyCategory>(); // I've ever tried removing this line.
Mapper.CreateMap<List<CompanyCategoryViewModel>, List<CompanyCategory>>();
Controller :
Mapper.Map(companyViewModel, company);
公司对象 = 动态代理 CompanyCategories 列出(公司的)子对象、 map ,但仍然不是动态代理。
最佳答案
您需要 Child
的映射对象类型,也用于集合。如果你有 List<>
你需要指定 List<>
在你的CreateMap
(它不适用于 IEnumerable
Mapper.CreateMap<ViewModel, TestObjectType>();
Mapper.CreateMap<ViewModelChildView, TestObjectChildType>();
Mapper.CreateMap<List<ViewModelChildView>>, List<TestObjectChildType>>();
这是一个GIST包含我的重现代码。
为了完成图片,我使用了最新的 AutoMapper 预实现版本 2.2.1-ci9004.
关于c# - AutoMapper - 映射子列表时非常奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14756530/