entity framework+ AutoMapper(实体到DTO和DTO到实体)

我在使用EF和AutoMapper时遇到了一些问题。 = /

例如 :

我有2个相关实体(客户和订单),他们是DTO课程:

class CustomerDTO { public string CustomerID {get;set;} public string CustomerName {get;set;} public IList Orders {get;set;} } class OrderDTO { public string OrderID {get;set;} public string OrderDetails {get;set;} public CustomerDTO Customers {get;set;} } //when mapping Entity to DTO the code works Customers cust = getCustomer(id); Mapper.CreateMap(); Mapper.CreateMap(); CustomerDTO custDTO = Mapper.Map(cust); //but when i try to map back from DTO to Entity it fails with AutoMapperMappingException. Mapper.Reset(); Mapper.CreateMap(); Mapper.CreateMap(); Customers customerModel = Mapper.Map(custDTO); // exception is thrown here 

class CustomerDTO { public string CustomerID {get;set;} public string CustomerName {get;set;} public IList Orders {get;set;} } class OrderDTO { public string OrderID {get;set;} public string OrderDetails {get;set;} public CustomerDTO Customers {get;set;} } //when mapping Entity to DTO the code works Customers cust = getCustomer(id); Mapper.CreateMap(); Mapper.CreateMap(); CustomerDTO custDTO = Mapper.Map(cust); //but when i try to map back from DTO to Entity it fails with AutoMapperMappingException. Mapper.Reset(); Mapper.CreateMap(); Mapper.CreateMap(); Customers customerModel = Mapper.Map(custDTO); // exception is thrown here

class CustomerDTO { public string CustomerID {get;set;} public string CustomerName {get;set;} public IList Orders {get;set;} } class OrderDTO { public string OrderID {get;set;} public string OrderDetails {get;set;} public CustomerDTO Customers {get;set;} } //when mapping Entity to DTO the code works Customers cust = getCustomer(id); Mapper.CreateMap(); Mapper.CreateMap(); CustomerDTO custDTO = Mapper.Map(cust); //but when i try to map back from DTO to Entity it fails with AutoMapperMappingException. Mapper.Reset(); Mapper.CreateMap(); Mapper.CreateMap(); Customers customerModel = Mapper.Map(custDTO); // exception is thrown here

class CustomerDTO { public string CustomerID {get;set;} public string CustomerName {get;set;} public IList Orders {get;set;} } class OrderDTO { public string OrderID {get;set;} public string OrderDetails {get;set;} public CustomerDTO Customers {get;set;} } //when mapping Entity to DTO the code works Customers cust = getCustomer(id); Mapper.CreateMap(); Mapper.CreateMap(); CustomerDTO custDTO = Mapper.Map(cust); //but when i try to map back from DTO to Entity it fails with AutoMapperMappingException. Mapper.Reset(); Mapper.CreateMap(); Mapper.CreateMap(); Customers customerModel = Mapper.Map(custDTO); // exception is thrown here

难道我做错了什么?

提前致谢 !

我遇到的问题与EntityCollection引用的更新有关。 当从DTO映射到实体时,AutoMapper会创建关系的新实例,而不会取悦EF。

解决了我的问题是配置AutoMapper以使用我的EntityCollection属性的目标值。 在你的情况下:

 Mapper.CreateMap< CustomerDTO , Customers >().ForMember(c => c.Orders, o => o.UseDestinationValue()); 

这样AM将不会创建新的EntityCollection实例,并将使用原始Customer实体附带的实例。

我仍然在努力实现自动化,但现在它解决了我的问题。

您的问题是因为Automapper丢失了与记录关联的EntityKey。 由于EntityFramework默认情况下不处理POCO(Plain Old CLR Object)

Jay Zimmerman在这里有一个很好的例子,说明如何处理这个问题。 gd / 4NIcj同样来自Jaroslaw Kowalski(我相信EF团队的一部分)有一个在EF中使用POCO的例子,它可以很好地转换为与Automapper一起使用(我还没有机会尝试它): http:/ /blogs.msdn.com/jkowalski/archive/2008/09/09/persistence-ignorance-poco-adapter-for-entity-framework-v1.aspx

我不确定你的问题是什么,但是 – 当我想使用LINQToEntities(切换到NHibernate)时,
我成功地使用了automapper。

看看代码:

 public class SimpleMapper { public static TTo Map(TFrom fromModel) { Mapper.CreateMap(); return Mapper.Map(fromModel); } public static IList MapList(IList fromModel) { Mapper.CreateMap(); return Mapper.Map, IList>(fromModel); } } public class RepositoryBase { public IList Map(IList model) { return SimpleMapper.MapList(model); } public TModel Map(TLINQModel model) { return SimpleMapper.Map(model); } public TLINQModel Map(TModel model) { return SimpleMapper.Map(model); } public IList Map(IList model) { return SimpleMapper.MapList(model); } public IList Map(IList model) { return SimpleMapper.MapList(model); } } 

它非常神秘,总是重新创建映射,但它确实有效。 我希望它有所帮助。 🙂

尝试映射到现有对象:

 entity = Mapper.Map(dto, entity); 

并将Ignore()保持在原位。

http://groups.google.com/group/automapper-users/browse_thread/thread/24a90f22323a27bc?fwc=1&pli=1

现在,使用新版本的AutoMapper,推荐的方法是使用Queryable-Extensions :

当使用ORM(如NHibernate或Entity Framework)和AutoMapper的标准Mapper.Map函数时,您可能会注意到当AutoMapper尝试将结果映射到目标类型时,ORM将查询图中所有对象的所有字段

如果您的ORM暴露了IQueryables,您可以使用AutoMapper的QueryableExtensions帮助程序方法来解决这个关键难题。

.ProjectTo()将告诉AutoMapper的映射引擎向IQueryable发出一个select子句,该子句将通知entity framework它只需要查询Item表的Name列,就像手动将IQueryable手动投影到OrderLineDTO一样。选择条款。

创建映射:

 Mapper.CreateMap(); 

并将项目查询到dto:

 var customerDto = session.Query().Where(customer => customer.Id == id) .Project().To() .Single(); 

在映射错误方面,AutoMapper非常具有表现力。 仔细阅读exception消息。

另一个重要的事情是要记住调用Mapper.AssertConfigurationIsValid(); 创建映射后。 如果映射错误,则会出错,从而在应用程序运行时稍后阻止exception。

您应该忽略某些实体属性的映射,如下所示:

 Mapper.CreateMap() .ForMember(dest => dest.EntityKey, opt => opt.Ignore()) .ForMember(dest => dest.Licenses, opt => opt.Ignore()) .ForMember(dest => dest.AccessCodes, opt => opt.Ignore()); 

如果检查来自Automapper抛出的exception的消息,您应该看到无法映射的实体属性,并如上所述忽略它们。

你可以在这里阅读,你需要做以下事情

您可以使用AutoMapper更新实体。 方法如下:将DTO和实体对象传递给AutoMapper的Map方法。 这就是这段代码的作用:

 custExisting = Mapper.Map(Of CustomerDTO, Customer)(custDTO, custExisting) 

还要注意映射问题,如此处所述

这些提示对我有用。