简体   繁体   English

AutoMapper - 映射子列表时非常奇怪的行为

[英]AutoMapper - Very strange behaviour when mapping child lists

This is most certainly a peculiar issue, and after finding nobody out there with the same issue, I hope I can find someone who actually knows what the problem is. 这当然是一个特殊的问题,在找到同样问题的人后,我希望我能找到一个真正知道问题所在的人。

I've got, say: 我有,说:

public class ViewModel
{
     public string TestString { get; set; }
     public ChildObject Child { get; set; }
     public List<Children> Children { get; set; }
}

Now, when I go to mapping my objects in my controller... ie 现在,当我在控制器中映射我的对象时...即

TestObject testObject = Repository.GetObject() // This is my Entity Framework object (with dynamic proxies attached to it)
Mapper.Map(ViewModel, testObject);

and, my mappings are defined as: 而且,我的映射定义为:

Mapper.CreateMap<ViewModel, TestObjectType>();
Mapper.CreateMap<ViewModelChildView, TestObjectChildType>();

Everything maps fine, so I've got my ViewModel setup properly, and my form. 一切都映射得很好,所以我已经正确设置了ViewModel和我的表单。 The problem though? 但问题呢? That's the interesting part. 这是有趣的部分。

ChildObject is, as the my original objects are, a DynamicProxy (Entity Framework). 作为我的原始对象, ChildObjectChildObject (实体框架)。 The List<Children> however, gets set to a normal List (even when the original list is a DynamicProxy of List<Children> ). 然而, List<Children>被设置为普通List(即使原始列表是List<Children>的DynamicProxy)。

This, in turn, means Entity Framework can't access this list as it's not bound to the context properly. 反过来,这意味着实体框架无法访问此列表,因为它未正确绑定到上下文。 I've tried all day to find a workaround with no luck. 我整天都试着找一个没有运气的解决方法。 Any ideas out there? 有什么想法吗?

Edit 编辑

These are my actual object bindings: 这些是我的实际对象绑定:

 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);

Company object = Dynamic Proxy CompanyCategories list sub object (of Company), maps, but still not a Dynamic Proxy. 公司对象=动态代理公司类别列出子对象(公司),地图,但仍然不是动态代理。

You need both the mapping for the Child object type and also for the collection. 您既需要Child对象类型的映射,也需要集合的映射。 If you have a List<> you need to specify List<> in your CreateMap (it won't work with IEnumerable 如果你有一个List<>你需要在CreateMap指定List<> (它不适用于IEnumerable

Mapper.CreateMap<ViewModel, TestObjectType>();
Mapper.CreateMap<ViewModelChildView, TestObjectChildType>();
Mapper.CreateMap<List<ViewModelChildView>>, List<TestObjectChildType>>();

Here is a GIST containing my repro code. 这是一个包含我的repro代码的GIST

To complete the picture I've used the latest pre-realise version of AutoMapper 2.2.1-ci9004. 为了完成图片,我使用了AutoMapper 2.2.1-ci9004.的最新预实现版本2.2.1-ci9004.

So basically the only difference i think is how you create the maps. 所以基本上我认为唯一的区别就是你如何创建地图。

perhaps next to 也许旁边

   Mapper.CreateMap<ViewModelChildView, TestObjectChildType>();

you should also create a list mapping like 你还应该创建一个列表映射

   Mapper.CreateMap<IEnumerable<ViewModelChildView>, IEnumerable<TestObjectChildType>>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM