Using Automapper to map entity framework classes to business classes

automapper net core
automapper map list
error mapping types automapper c#
automapper map dto to entity
missing type map configuration or unsupported mapping.
automapper map child collection
automapper error mapping types list
automapper best practices

I have the following two classes generated by Entity Framework:

public partial class Person
{
    public int id { get; set; }
    public string namen { get; set; }
    public int house { get; set; }
    [IgnoreMap]
    public virtual House House1 { get; set; }
}

public partial class House
{
    public House()
    {
        this.Persons = new HashSet<Person>();
    }
    public int id { get; set; }
    public string street { get; set; }
    public string city { get; set; }
    public ICollection<Person> Persons { get; set; }
}

Then I also have these two similar classes in my Business Layer:

public class House
{        
    public House()
    {
        this.Persons = new HashSet<Person>();
    }
    public int id { get; set; }
    public string street { get; set; }
    public string city { get; set; }        
    public virtual ICollection<Person> Persons { get; set; }
}

public class Person
{
    public int id { get; set; }
    public string namen { get; set; }
    public int house { get; set; }        
}

Pretty much the same, hugh? In my Business Layer I read a List of houses from the Database. Then I map the whole list to a list of my Business house class using Automapper:

    public List<elci.BusinessEntities.House> getHouses()
    {
        YardEntities cx = new YardEntities();
        Mapper.Initialize(cfg => cfg.CreateMap<DataAccessLayer.House, BusinessEntities.House>());

        List<DataAccessLayer.House> dhl = cx.Houses.ToList();
        List<BusinessEntities.House> bhl = Mapper.Map<List<DataAccessLayer.House>, List<BusinessEntities.House>>(dhl);
        return bhl;
    }

However, in the following line I get a runtime exception:

 Mapper.Map<List<DataAccessLayer.House>, List<BusinessEntities.House>>(dhl);

"Error mapping types".

I guess, that this might be, because every Person points to a House and every House points to Persons. Because I do not need this "circle" in my BusinessLayer I decorated this attribute with [IgnoreMap], but without any success. The error still remains.

Any suggest what I am doing wrong?

So this eventually fixed my issue:

        Mapper.Initialize(cfg => {
            cfg.CreateMap<List<House>, List<HouseViewModel>>();
            cfg.CreateMap<List<Person>, List<PersonViewModel>>();
        });

Mapping Entity Framework Entities to DTOs with AutoMapper, Because we want to use Entity Framework for the data-access layer and WCF we could just use the created Entity Framework Code First classes as In general, DTOs should contain only data and not business logic; it is� AutoMapper is a conventions-based mapping system that allows you to copy values from an instance of one class to an instance of another class using pre-defined maps. Once we've got AutoMapper set up, the mapping code becomes super simple: public TeamDTO GetByID(int id) { var team =

Yes, the error remains without ignoremap. The inner exception tells me the following:

{"Error mapping types.\r\n\r\nMapping types:\r\nList`1 -> List`1\r\nSystem.Collections.Generic.List`1[[elci.DataAccessLayer.House, DataAccessLayer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.List`1[[elci.BusinessEntities.House, BusinessEntities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"}

So the House-Type is the problem. I also tried to add another map:

        Mapper.Initialize(cfg => cfg.CreateMap<DataAccessLayer.House, BusinessEntities.House>());
        Mapper.Initialize(cfg => cfg.CreateMap<DataAccessLayer.Person, BusinessEntities.Person>());

without success and the same error. :-(

How to work with AutoMapper in C#, I have the following two classes generated by Entity Framework: public partial class Person { public int id { get; set; } public string namen { get; set; } public int� What's happening here is that Employee and Branch are being upcasted to Person and Business, respectively since those are the actual types in the PersonBusiness class. Then, when AutoMapper does the mapping, it sees Person and Business types and therefore maps them to PersonDto and BusinessDto as the configuration dictates.

Try to use

config.CreateMap<TSource,TDestination>().PreserveReferences()

You have circular references Person->House->ICollection

Circular references

Previously, AutoMapper could handle circular references by keeping track of what was mapped, and on every mapping, check a local hashtable of source/destination objects to see if the item was already mapped. It turns out this tracking is very expensive, and you need to opt-in using PreserveReferences for circular maps to work. Alternatively, you can configure MaxDepth:

// Self-referential mapping
cfg.CreateMap<Category, CategoryDto>().MaxDepth(3);
// Circular references between users and groups
cfg.CreateMap<User, UserDto>().PreserveReferences();

AutoMapper docs

Code using AutoMapper. To begin this article, let's define…, CSO > Two business people connecting and solving a puzzle. Note that AutoMapper can map any set of classes. Projections are used to map source values to a destination that does not match the structure of the source. Let us understand how to use automapper in C# with a simple example using a console application. We are going to map the same Employee class with EmployeeDTO class that we discussed in the first example. We need to map each Employee properties to the correspondent EmployeeDTO properties using AutoMapper as shown in the below image.

How to map Data Access to Business Logic objects in Entity , Automapper is a convention-based class mapping tool, meaning it relies to another object property (e.g, business object to data transfer object), to the database using Entity Framework by mapping each object one after� If you want to Use Plain Old Clr Objects in your Business Model Design and map them to your database tables, you may use the Code First Approach for Entity Framework. In Code first, nothing will be generated for you. However, you will have the responsability to map your Business Objects to your Database Tables and fields.

AutoMapper, I use EF6 all the time to generate my Data Access Layer from MSSQL tables, then I create a set (using your UserBusiness and UserDAL classes) This is generic base class for repository (note, that we use AutoMapper here): Id; return obj; } public TBLL Update(TBLL obj) { var entity = Map(obj); context. You make a folder called Automapper, and you put all the mapping classes in the folder, and you make the calls to AM to do the mapping. It's no more than that. So you shouldn't try to make a big deal out of it.

Why mapping DTOs to Entities using AutoMapper and , Automapper : mapping issue with inheritance and abstract base class on issue using AutoMapper (which is an excellent technology) to map a business object� Use AutoMapper to map one object to another in ASP.NET Core While developing ASP.NET Core web applications you often need to transfer data residing in one object into another. You can accomplish this task manually by setting properties of the target object with the values from the source object.

Comments
  • If you remove IgnoreMap attribute. Is it working?
  • Error Mapping Types error also gives what types are not mapped. can you get into the inner exception details and paste it here.
  • Also, you need to create a mapper for Person entities explicitly as they are referenced in House Entity.
  • As links can break, please include the relevant part of the linked page in the answer.
  • That's not needed anymore. The docs continue with Starting from 6.1.0 PreserveReferences is set automatically at config time whenever the recursion can be detected statically..