【发布时间】:2009-05-21 09:03:53
【问题描述】:
我在使用带有 AutoMapper 的 EF 时遇到了一些问题。 =/
例如:
我有 2 个相关实体(客户和订单) 它们是 DTO 类:
class CustomerDTO
{
public string CustomerID {get;set;}
public string CustomerName {get;set;}
public IList< OrderDTO > 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< Customers, CustomerDTO >();
Mapper.CreateMap< Orders, OrderDTO >();
CustomerDTO custDTO = Mapper.Map(cust);
//but when i try to map back from DTO to Entity it fails with AutoMapperMappingException.
Mapper.Reset();
Mapper.CreateMap< CustomerDTO , Customers >();
Mapper.CreateMap< OrderDTO , Orders >();
Customers customerModel = Mapper.Map< CustomerDTO ,Customers >(custDTO); // exception is thrown here
我做错了吗?
提前致谢!
【问题讨论】:
-
您的“客户”和“订单”实体是什么样的?如果没有看到它们的结构,很难说出发生了什么......
-
我也不能......在 entitycollection
和 dto 列表之间映射时遇到问题。 -
正如@geva30 所提到的,您应该在创建地图后致电
Mapper.AssertConfigurationIsValid()以确定您在运行时可能遇到的任何问题 -
你也可以试试 EntitiesToDTOs,一个实体框架 DTO 生成器,用作 Visual Studio 的插件:entitiestodtos.codeplex.com
标签: c# entity-framework automapper dto