开发者

Differents mode of mapping using AutoMapper

开发者 https://www.devze.com 2023-03-21 15:12 出处:网络
I\'m using AutoMapper to mapping my Entity Framework POCO objects and ViewModels. Question 1: What\'s difference in mapping using that:

I'm using AutoMapper to mapping my Entity Framework POCO objects and ViewModels.

Question 1:

What's difference in mapping using that:

Apurador apurador = Mapper.Map<NovoViewModel, Apurador>(novoViewModel);

or that:

Mapper.Map(novoViewModel, apurador);

Question 2:

When I had to update a Entity in Entity Framework using AutoMapper I had to do in that way:

Apurador apurador = context.Apurador.Find(id);
Mapper.Map(ed开发者_StackOverflowitarViewModel, apurador);
context.SaveChanges();

In that way, my entity don't lost the Id property, but in that way below the Id property is lost:

Apurador apurador = context.Apurador.Find(id);
apurador = Mapper.Map<EditarViewModel, Apurador>(editarViewModel);
context.SaveChanges();

Why??


first of all, does the last even compile? you instantiate two classes with the same name!

As for Apurador apurador = Mapper.Map<EditarViewModel, Apurador>(editarViewModel); what it does is it creates a new instance of Apurador with the property values from editarViewModel while Mapper.Map(editarViewModel, apurador); maps the property value from editarViewModel to the actual retrieved entity apurador (if I haven't gotten it mixed up).

0

精彩评论

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

关注公众号