简体   繁体   中英

Automapper Exception when mapping Entity Framework entity that contains a navigation property

I'm trying to map an EF entity to my service model using AutoMapper but I'm getting an error when the mapping occurs.

For example:

Service model class:

public class User
{
    public Guid UserId {get; set;}
    public string Name {get; set;}
    public Company Company {get; set;}
}

public class Company
{
    public Guid CompanyId {get; set;}
    public string Name {get; set;}
    public ICollection<User> Users {get; set;}
}

Entity model class:

public class UserData
{
    public Guid Id {get;set;}
    public string Name {get; set;}
    public Guid CompanyId CompanyId {get; set;}
    public virtual Company Company {get; set;}
}

public class CompanyData
{
    public Guid Id {get; set;}
    public string Name {get; set;}
    public virtual ICollection<UserData> Users {get; set;}
}

AutoMapper profiles

User Mapping

this.CreateMap<Data.Entities.UserData, Services.Models.User>()
    .ForMember(u=>u.UserId, opt=>opt.MapFrom(u=>u.Id))
    .ForMember(u=>u.Company, opt=>opt.MapFrom(u=>u.Company)); 

Company Mapping

this.CreateMap<Data.Entities.CompanyData, Services.Models.Company>()
   .ForMember(o => o.CompanyId, opt => opt.MapFrom(o => o.Id))
   .ForMember(o => o.Users, opt => opt.MapFrom(o => o.Users));

I get the following error:

AutoMapper.AutoMapperMappingException: 'Error mapping types.'
TypeLoadException: Method 'Add' in type
'Proxy_System.Collections.Generic.ICollection`1
[[MyCompany.Services.Models.User, MyCompany.Services, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null]]_19426640_' from assembly
'AutoMapper.Proxies, Version=0.0.0.0, Culture=neutral, 
PublicKeyToken=be96cd2c38ef1005' does not have an implementation.

If I remove the Users property from the service model and therefore it doesn't try to map the users when I'm trying to map a company it works fine. This also works fine when I'm mapping a user and it returns the company details along with it.

It clearly has something to do with the Users property but I'm not sure what.

Can someone tell me what I'm doing wrong?

Thanks.

I eventually figured the problem, well problems!

In my company profile, I had the comment out some part of the definition:

this.CreateMap<Data.Entities.Company, Models.Company>()
    .ForMember(o => o.OrganizationId, opt => opt.MapFrom(o => o.Id));
    //.ForMember(o => o.Users, opt => 
    //    opt.MapFrom((source,destination, member, context)=> source.Users != null ? 
    //    context.Mapper.Map<ICollection<Models.User>>(source.Users) : null));

In my user profile, I had the comment out some part of the definition:

    \\this.CreateMap<IEnumerable<Data.Entities.User>,
                    ICollection<Services.Models.User>>();

I know it is a different definition from my original post, leaving both these uncommented, generated a stack overflow and commenting the first one or the other caused the same error as my original question.

I made think over complicated to begin with which caused me problem when in fact AutoMapper handled all of it by itself!

This is my final code for the profile definitions:

public CompanyProfile()
{
    this.CreateMap<Data.Entities.Company, Models.Company>()
            .ForMember(o => o.CompanyId, opt => opt.MapFrom(o => o.Id));
}

and

this.CreateMap<Data.Entities.User, Services.Models.User>()
    .ForMember(u=>u.UserId, opt=>opt.MapFrom(u=>u.Id))
    .ForMember(u=>u.Company, opt=>opt.MapFrom(u=>u.Company));

This will load the company details from EF into my user model and it will load the users from EF in the company model.

And note that using ICollection works just fine.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM