Entity Framework + AutoMapper ( Entity to DTO and DTO to Entity )

后端 未结 8 1313
逝去的感伤
逝去的感伤 2021-01-30 07:38

I\'ve got some problems using EF with AutoMapper. =/

for example :

I\'ve got 2 related entities ( Customers and Orders ) and they\'re DTO classes :

         


        
8条回答
  •  情深已故
    2021-01-30 07:54

    I'm not sure what your problem is, but - when i wanted to use LINQToEntities (switched to NHibernate),
    i managed to use automapper with success.

    Take a look at code:

    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);
        }
    }
    

    It's quite cryptic, always recreates mappings, but it worked. I hope it helps somehow. :)

提交回复
热议问题