简体   繁体   English

映射包含导航属性的实体框架实体时,Automapper异常

[英]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. 我正在尝试使用AutoMapper将EF实体映射到我的服务模型,但是在发生映射时出现错误。

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 AutoMapper配置文件

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. 如果我从服务模型中删除了Users属性,因此当我尝试映射公司时,它不尝试映射用户,则可以正常工作。 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. 显然,它与Users属性有关,但是我不确定。

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! 实际上,我想起来很复杂,这实际上使AutoMapper自行处理了所有问题,这给我带来了麻烦!

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. 这会将EF的公司详细信息加载到我的用户模型中,并将EF的用户加载到公司模型中。

And note that using ICollection works just fine. 请注意,使用ICollection可以正常工作。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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