开发者

automapper doesn't map as expected

开发者 https://www.devze.com 2023-02-13 10:57 出处:网络
I am new to automapper, so I may be attacking this the wrong way.I am working on an mvc app and I want to use automapper to map between my domain models and my view models.My view is taking an IList t

I am new to automapper, so I may be attacking this the wrong way. I am working on an mvc app and I want to use automapper to map between my domain models and my view models. My view is taking an IList to render the html page.

Here is the code from my controller -

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

Here is my Automapper Configuration -

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

Here is my Entit开发者_JAVA百科y1

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

       public virtual Entity2 entity2 { get; set; }
}

Here is entity2 -

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

Here is my 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.

I've seen the post here that shows that there should be an entity2Dto object to map to, but where does that tie in? Do I have to add that to my viewModel object?

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.

Interesting Entity2Everything also appears to work - I am not sure if this is by design or a bug in the AutoMapper code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号