简体   繁体   English

Automapper的“忽略”选项将目标属性设置为NULL

[英]Automapper's Ignore option setting destination property to NULL

In my Global.asax: 在我的Global.asax中:

        AutoMapper.Mapper.CreateMap<ViewModel, DataType>()
            .ForMember(dest => dest.User, opt => opt.Ignore());

In my MVC Action: 在我的MVC操作中:

        AutoMapper.Mapper.Map(viewmodel, data);

Prior to calling the Map() function, data.User is not null. 在调用Map()函数之前,data.User不为null。 Immediately after calling Map(), data.User is now null, however, if I am understanding correctly, Ignore() should completely ignore that property and leave it untouched. 在调用Map()之后,data.User现在立即为null,但是,如果我正确理解,则Ignore()应该完全忽略该属性,并且保持不变。 Any ideas as to what might be causing this? 关于什么可能导致此的任何想法?

I clearly didn't give enough information for anyone to solve this. 我显然没有给任何人提供足够的信息来解决这个问题。

My problem happened to occur because my data model is an EF4 Entity, and my View Model had a mapping to a Foreign Key that was mapped to the User object mentioned above. 因为我的数据模型是EF4实体,并且我的视图模型具有到映射到上述User对象的外键的映射,所以发生了我的问题。 Once we call Map(), it reset the User entity to null. 调用Map()后,它将User实体重置为null。 Once I removed this property from my ViewModel, everything began working as expected. 一旦从ViewModel中删除了该属性,一切就会按预期开始工作。

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

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