简体   繁体   English

自动映射器未按预期映射

[英]automapper doesn't map as expected

I am new to automapper, so I may be attacking this the wrong way. 我是Automapper的新手,所以我可能以错误的方式进行了攻击。 I am working on an mvc app and I want to use automapper to map between my domain models and my view models. 我正在开发一个mvc应用程序,我想使用自动映射器在域模型和视图模型之间进行映射。 My view is taking an IList to render the html page. 我的观点是采用IList呈现html页面。

Here is the code from my controller - 这是我的控制器的代码-

IList<viewModel> vm = Mapper.Map<IList<entity1, viewModel>(dataFromEntity1);

Here is my Automapper Configuration - 这是我的Automapper配置-

 protected override void Configure()
 {
     CreateMap<entity1, viewModel>();
 }

Here is my Entity1 这是我的实体1

public class entity1
{
       public virtual int id { get; set; }
       public virtual string anything { get; set; }

       public virtual Entity2 entity2 { get; set; }
}

Here is entity2 - 这是entity2-

public class entity2
{
       public virtual int id_2 { get; set; }
       public virtual string everything { get; set; }
}

Here is my viewModel - 这是我的viewModel-

public class viewModel
{
       public string anything { get; set; }
       public string entity2everything { get; set; }
}

From reading through the docs on codeplex, I assumed the mapping for everything from entity2 would work automatically, but when I run the test AssertConfigurationIsValid, it fails on entity2everything. 通过阅读Codeplex上的文档,我假设从entity2进行的所有映射都可以自动工作,但是当我运行测试AssertConfigurationIsValid时,它对entity2everything失败。

I've seen the post here that shows that there should be an entity2Dto object to map to, but where does that tie in? 我在这里看到的帖子显示应该有一个object2Dto对象要映射到,但是该链接在哪里? Do I have to add that to my viewModel object? 我是否必须将其添加到我的viewModel对象中?

Am I a missing a mapping somewhere? 我在某处缺少映射吗? I can't figure out how this is supposed to work. 我不知道这应该如何工作。 Thanks for any thoughts. 感谢您的任何想法。

The problem appears to be related to the property name casing! 这个问题似乎与属性名称外壳!

If you rename the view model property to entity2Everything (instead of entity2everything) it should work. 如果将视图模型属性重命名为entity2Everything(而不是entity2everything),则它应该起作用。

Interesting Entity2Everything also appears to work - I am not sure if this is by design or a bug in the AutoMapper code. 有趣的Entity2Everything似乎也可以工作-我不确定这是设计使然还是AutoMapper代码中的错误。

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

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