Automapper EF6 Dynamic Proxy
We are developing an EF6 web project Code first. After reviewing a lot of different approaches for our project we have decided to divide our solution into these projects:
In the service layer we create DTOs for each Entity we need to send to the web layer. SO we have the Entities from the model layer, then we convert those entities to our DTOs in the service layer and send those DTOs to the web layer. Finally in the web layer we get those DTOs and convert them to our ModelViews we use in our Views.
This way we separate all layers and the Web layer doesn't know about our Model layer.
We use AUtoMapper for all the mapping from Entities->DTOs->ModelViews
My question is, AutoMapper works perfect in the Web layer because it receives a DTO object and converts it to ModelView object.
The problem is in our service layer bacuse we are receiving a DynamicPrixies object instead of a Entity object so AUtoMapper gives us an error:
var myEntity = Dbset.FirstOrDefault(p => p.Id == id);
var myDto = Mapper.Map<LinkDto>(myEntity);
Of course we do have the proper mapping for that conversion (we have tried with a created Entity object) and we can't set the ProxyCreationEnabled = false because we use lazy loading in our queries.
We are using the last version of AutoMapper (5.0.2) and we are creating maps like this:
In the Global.asax
Mapper.Initialize(
cfg => {
cfg.AddProfile(new AutoMapperWebProfile());
cfg.AddProfile(new AutoMapperServiceProfile());
cfg.IgnoreUnmapped();
});
Mapper.AssertConfigurationIsValid();
In the Web project we have our profile for that project and in the Service project we have the other profile:
public AutoMapperServiceProfile()
{
CreateMap<LinkDto, Link>().ReverseMap();
}
Is there any solution to:
A) Convert the DynamicProxy that the query generates to an Entity object?
B) Tell AutoMapper to read those DynamicProxies?
链接地址: http://www.djcxy.com/p/37432.html上一篇: 将字符串转换为枚举
下一篇: Automapper EF6动态代理