6
votes

I'm using EntityFramework as a DataLayer and DTO to transfer data between layer. I develop Windows Forms in N-Tier architecture and when I try to mapping 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've got this error: http://s810.photobucket.com/user/sky3913/media/AutoMapperError.png.html

The error said that I missing type map configuration or unsupported mapping. I have registered mapping using profile in this way at UI Layer:

[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've got the same error too when mapping from DTO to Entity.

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

How to solve this?

3
instead of var categoriesDTO = Mapper.Map<IEnumerable<Category>, List<CategoryDTO>>(categories); can you try mapping them one at a time in a loop using var categoryDto = Mapper.Map<CategoryDTO>(category) ?wal
@wal I've got the same error "Missing type map configuration or unsupported mapping"Willy
instead of using the profile can you just see what happens if you try creating the mapping prior to the actual map. eg call Mapper.CreateMap<Category, CategoryDTO>(); then directly after do your mapping; dont map a list that comes back from entity framework; instead loop and map individually.wal
I remove the profile, and define Mapper.CreateMap<Category, CategoryDTO>() before do Mapper.Map, and it works. I think because the configuration using profile did not configure correctly. Actually I want to centralize automapper configuration so I'm using profile. Any idea how to achive this in ntier architecture?Willy
Its because you are using Mapper.Initialize multipe times. If you look at the source code for this call it calls Mapper.Reset() which means only the last mapping defined will work. so instead simply remove the Initialize calls and replace with Mapper.AddProfile< >wal

3 Answers

4
votes

Its because you are using Mapper.Initialize multiple times. If you look at the source code it calls Mapper.Reset() which means only the last mapping defined will work. so instead simply remove the Initialize calls and replace with Mapper.AddProfile< >

2
votes

Use AutoMapper.AssertConfigurationIsValid() after the Configure() calls. If anything fails it will throw an exception with a descriptive text. It should give you more info to debug further.

1
votes

Mapping DTOs to Entities using AutoMapper and EntityFramework

here we have an Entity class Country and an 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 Object of CountryDTO

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 above code will map CountryDTO to Country Entity Object and add new entities to the 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