Error matching from Entity to DTO or DTO to Entity using AutoMapper

I use EntityFramework as a DataLayer and DTO to transfer data between layers. I am developing Windows Forms in an N-tier architecture and when trying to map from Entity to DTO in BLL:

public IEnumerable<CategoryDTO> GetCategoriesPaged(int skip, int take, string name) { var categories = unitOfWork.CategoryRepository.GetCategoriesPaged(skip, take, name); var categoriesDTO = Mapper.Map<IEnumerable<Category>, List<CategoryDTO>>(categories); return categoriesDTO; } 

I have this error: http://s810.photobucket.com/user/sky3913/media/AutoMapperError.png.html

The error indicates that I do not have a type map configuration or an unsupported display. I registered the mapping using the profile this way at the user interface level:

 [STAThread] static void Main() { AutoMapperBusinessConfiguration.Configure(); AutoMapperWindowsConfiguration.Configure(); ... Application.Run(new frmMain()); } 

and AutoMapper configuration is in BLL:

 public class AutoMapperBusinessConfiguration { public static void Configure() { Mapper.Initialize(cfg => { cfg.AddProfile<EntityToDTOProfile>(); cfg.AddProfile<DTOToEntityProfile>(); }); } } public class EntityToDTOProfile : Profile { public override string ProfileName { get { return "EntityToDTOMappings"; } } protected override void Configure() { Mapper.CreateMap<Category, CategoryDTO>(); } } public class DTOToEntityProfile : Profile { public override string ProfileName { get { return "DTOToEntityMappings"; } } protected override void Configure() { Mapper.CreateMap<CategoryDTO, Category>(); } } 

I also have the same error when comparing with DTO on Entity.

 category = Mapper.Map<Category>(categoryDTO); 

How to solve this?

+6
source share
3 answers

This is because you use Mapper.Initialize several times. If you look at the source code, it calls Mapper.Reset() , which means that only the last displayed mapping will work. so instead, just remove the Initialize calls and replace with Mapper.AddProfile< >

+4
source

Use AutoMapper.AssertConfigurationIsValid() after calls to Configure() . If something fails, it will throw an exception with descriptive text. It should give you more information for further debugging.

+2
source

DTO mapping for objects using AutoMapper and EntityFramework

here we have an Entity class class and CountryDTO

  public class Country { public int CountryID { get; set; } public string ContryName { get; set; } public string CountryCode { get; set; } } 

Countrydto

  public class CountryDTO { public int CountryID { get; set; } public string ContryName { get; set; } public string CountryCode { get; set; } } 

Create CountryDTO Object

 CountryDTO collection=new CountryDTO(); collection.CountryID =1; collection.ContryName ="India"; collection.CountryCode ="in"; Country model = Convertor.Convert<Country, CountryDTO>(collection); dbcontext.Countries.Add(model); dbcontext.SaveChanges(); 

this will work fine for a new country, the code above will map CountryDTO to an Entity Country object and add new objects to dbcontext and save the changes.

 using System.Reflection; public static TOut Convert<TOut, TIn>(TIn fromRecord) where TOut : new() { var toRecord = new TOut(); PropertyInfo[] fromFields = null; PropertyInfo[] toFields = null; fromFields = typeof(TIn).GetProperties(); toFields = typeof(TOut).GetProperties(); foreach (var fromField in fromFields) { foreach (var toField in toFields) { if (fromField.Name == toField.Name) { toField.SetValue(toRecord, fromField.GetValue(fromRecord, null), null); break; } } } return toRecord; } public static List<TOut> Convert<TOut, TIn>(List<TIn> fromRecordList) where TOut : new() { return fromRecordList.Count == 0 ? null : fromRecordList.Select(Convert<TOut, TIn>).ToList(); } 

http://bhupendrasinghsaini.blogspot.in/2014/09/convert-enity-framwork-data-in-entity.html

+1
source

Source: https://habr.com/ru/post/972528/


All Articles