实体框架+ AutoMapper(实体到DTO和DTO到实体)

shk*_*per 20 c# entity-framework dto automapper

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

例如 :

我有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

难道我做错了什么?

提前致谢 !

Pab*_*lla 18

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

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

Mapper.CreateMap< CustomerDTO , Customers >().ForMember(c => c.Orders, o => o.UseDestinationValue());
Run Code Online (Sandbox Code Playgroud)

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

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


ish*_*kci 6

尝试映射到现有对象:

entity = Mapper.Map<MyDTO, NyEntity>(dto, entity); 
Run Code Online (Sandbox Code Playgroud)

并保持 Ignore() 的位置。

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


小智 5

您的问题是因为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


Arn*_*psa 4

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

看一下代码:

public class SimpleMapper<TFrom, TTo>
{
    public static TTo Map(TFrom fromModel)
    {
        Mapper.CreateMap<TFrom, TTo>();
        return Mapper.Map<TFrom, TTo>(fromModel);
    }

    public static IList<TTo> MapList(IList<TFrom> fromModel)
    {
        Mapper.CreateMap<TFrom, TTo>();
        return Mapper.Map<IList<TFrom>, IList<TTo>>(fromModel);
    }
}

public class RepositoryBase<TModel, TLINQModel>
{
    public IList<TModel> Map<TCustom>(IList<TCustom> model)
    {
        return SimpleMapper<TCustom, TModel>.MapList(model);
    }

    public TModel Map(TLINQModel model)
    {
        return SimpleMapper<TLINQModel, TModel>.Map(model);
    }

    public TLINQModel Map(TModel model)
    {
        return SimpleMapper<TModel, TLINQModel>.Map(model);
    }

    public IList<TModel> Map(IList<TLINQModel> model)
    {
        return SimpleMapper<TLINQModel, TModel>.MapList(model);
    }

    public IList<TLINQModel> Map(IList<TModel> model)
    {
        return SimpleMapper<TModel, TLINQModel>.MapList(model);
    }
}
Run Code Online (Sandbox Code Playgroud)

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