本文介绍了具有很多对很多和IncludeMembers的自动映射器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的班级

  public class Account
    {
       public int Id { get; set; }

       public string AccountNumber { get; set; } = null!;
       ..other fields

       public  SubAccount1 SubAccount1 { get; set; } = null!;
       public  SubAccount2 SubAccount2 { get; set; } = null!;
      public ICollection<AccountCustomer> AccountAndCustomers { get; set; } = new List<AccountCustomer>();   ///Many to many mapping
    }

   public class AccountCustomer
   {
        public int AccountId { get; set; }
        public Account Account { get; set; } = null!;

        public int CustomerId { get; set; }
        public Customer Customer { get; set; } = null!;
        ...other fields
   }

 public class SubAccount1
    {
       public int AccountId { get; set; }
       public  Account Account { get; set; } = null!;
       public string  Subfield1 { get; set; } = null!;
        ..other fields
    }

    public class SubAccount2
    {
       public int AccountId { get; set; }
       public  Account Account { get; set; } = null!;
       public string  Subfield2 { get; set; } = null!;
        ..other fields
    }


    public class SubAccount1DTO
    {

       public int AccountId { get; set; }

       public string AccountNumber { get; set; } = null!;

       public string  Subfield1 { get; set; } = null!;

       public IReadOnlyList<CustomerDTO> Customers { get; set; } = new List<CustomerDTO>();
    }
public class CustomerDTO
    {

        public int Id { get; set; }
        public string CustomerNo { get; set; } = null!;

        public string FirstName { get; set; } = null!;
         ..other fillds

       }

public class Customer
    {


       public int Id { get; set; }
       public string? CustomerNo { get; set; } = null!;
       ....other filds
       public ICollection<AccountCustomer> AccountAndCustomers { get; set; } = new List<AccountCustomer>();

}

基于此链接的自动映射器配置

CreateMap<Customer, CustomerDTO>().ReverseMap();

CreateMap<Account, SubAccount1DTO>()
                .IncludeMembers(s => s.SubAccount1)
                .ForMember(d => d.Customers, opt => opt.MapFrom(s => s.AccountAndCustomers))   ///After include Customers related am getting error
                 .ReverseMap()
                  .AfterMap((s, d) =>
                  {
                      foreach (var accountCustomer in d.AccountAndCustomers)
                          accountCustomer.AccountId = s.AccountId ;
                  });

    CreateMap<SubAccount1, SubAccount1DTO>(MemberList.None)
                  .ForMember(dest => dest.Id, opt => opt.Ignore())
                  .ForMember(dest => dest.AccountNumber, opt => opt.Ignore())
                  .ReverseMap();

 CreateMap<Customer, AccountCustomer>()  ----i dnt no whether this to included
            .ForMember(dest => dest.CustomerId, opt => opt.MapFrom(src => src.Id))
            .ForMember(dest => dest.Customer, opt => opt.MapFrom(src => src))
            .ForMember(dest => dest.AccountId, opt => opt.Ignore())
            .ForMember(dest => dest.Account, opt => opt.Ignore())
            ;

我需要将Account映射到SubAccount1DTO和Reversemap.这里SubAccount1DTO拥有customersdto的客户列表,但是当我在下面的行中添加

.ForMember(d => d.Customers,opt => opt.MapFrom(s => s.AccountAndCustomers))

我遇到以下错误..请提出

发生一个或多个错误. (无法映射Application.DTOs.Accounts.SubAccount1DTO上的以下成员:顾客添加一个自定义映射表达式,忽略,添加一个自定义解析器,或修改目标类型Application.DTOs.Accounts.SubAccount1DTO.语境:从Domain.Entities.Accounts.Account到Application.DTOs.Accounts.SubAccount1DTO映射到成员客户

解决方案

以下映射

.ForMember(d => d.Customers, opt => opt.MapFrom(s => s.AccountAndCustomers))

将类型为ICollection<AccountCustomer>的源属性映射到类型为IReadOnlyList<CustomerDTO>的目标属性.

AutoMapper允许这样做,但需要您创建集合元素类型的映射-在这种特定情况下,是AccountCustomerCustomerDTO(在链接的帖子中,从BookCategoryCategoryDto的映射也类似)./p>

因此,您需要的最低要求是在映射器配置中添加以下内容:

CreateMap<AccountCustomer, CustomerDTO>()
    .IncludeMembers(s => s.Customer);

Below is my Class

  public class Account
    {
       public int Id { get; set; }

       public string AccountNumber { get; set; } = null!;
       ..other fields

       public  SubAccount1 SubAccount1 { get; set; } = null!;
       public  SubAccount2 SubAccount2 { get; set; } = null!;
      public ICollection<AccountCustomer> AccountAndCustomers { get; set; } = new List<AccountCustomer>();   ///Many to many mapping
    }

   public class AccountCustomer
   {
        public int AccountId { get; set; }
        public Account Account { get; set; } = null!;

        public int CustomerId { get; set; }
        public Customer Customer { get; set; } = null!;
        ...other fields
   }

 public class SubAccount1
    {
       public int AccountId { get; set; }
       public  Account Account { get; set; } = null!;
       public string  Subfield1 { get; set; } = null!;
        ..other fields
    }

    public class SubAccount2
    {
       public int AccountId { get; set; }
       public  Account Account { get; set; } = null!;
       public string  Subfield2 { get; set; } = null!;
        ..other fields
    }


    public class SubAccount1DTO
    {

       public int AccountId { get; set; }

       public string AccountNumber { get; set; } = null!;

       public string  Subfield1 { get; set; } = null!;

       public IReadOnlyList<CustomerDTO> Customers { get; set; } = new List<CustomerDTO>();
    }
public class CustomerDTO
    {

        public int Id { get; set; }
        public string CustomerNo { get; set; } = null!;

        public string FirstName { get; set; } = null!;
         ..other fillds

       }

public class Customer
    {


       public int Id { get; set; }
       public string? CustomerNo { get; set; } = null!;
       ....other filds
       public ICollection<AccountCustomer> AccountAndCustomers { get; set; } = new List<AccountCustomer>();

}

Auto mapper configuration based on this link Automapper many to many

CreateMap<Customer, CustomerDTO>().ReverseMap();

CreateMap<Account, SubAccount1DTO>()
                .IncludeMembers(s => s.SubAccount1)
                .ForMember(d => d.Customers, opt => opt.MapFrom(s => s.AccountAndCustomers))   ///After include Customers related am getting error
                 .ReverseMap()
                  .AfterMap((s, d) =>
                  {
                      foreach (var accountCustomer in d.AccountAndCustomers)
                          accountCustomer.AccountId = s.AccountId ;
                  });

    CreateMap<SubAccount1, SubAccount1DTO>(MemberList.None)
                  .ForMember(dest => dest.Id, opt => opt.Ignore())
                  .ForMember(dest => dest.AccountNumber, opt => opt.Ignore())
                  .ReverseMap();

 CreateMap<Customer, AccountCustomer>()  ----i dnt no whether this to included
            .ForMember(dest => dest.CustomerId, opt => opt.MapFrom(src => src.Id))
            .ForMember(dest => dest.Customer, opt => opt.MapFrom(src => src))
            .ForMember(dest => dest.AccountId, opt => opt.Ignore())
            .ForMember(dest => dest.Account, opt => opt.Ignore())
            ;

I need to map Account to SubAccount1DTO and Reversemap. Here SubAccount1DTO has list of customersdto But when i included below line

.ForMember(d => d.Customers, opt => opt.MapFrom(s => s.AccountAndCustomers))

i am getting below error .. Please suggest

One or more errors occurred. (The following member on Application.DTOs.Accounts.SubAccount1DTO cannot be mapped:CustomersAdd a custom mapping expression, ignore, add a custom resolver, or modify the destination type Application.DTOs.Accounts.SubAccount1DTO.Context:Mapping to member Customers from Domain.Entities.Accounts.Account to Application.DTOs.Accounts.SubAccount1DTO

解决方案

The following mapping

.ForMember(d => d.Customers, opt => opt.MapFrom(s => s.AccountAndCustomers))

maps source property of type ICollection<AccountCustomer> to destination property of type IReadOnlyList<CustomerDTO>.

AutoMapper allows that, but requires you to create map for collection element types - in this specific case, AccountCustomer to CustomerDTO (in the linked post there is similar mapping from BookCategory to CategoryDto).

Hence the minimum you need here is adding something like this to the mapper configuration:

CreateMap<AccountCustomer, CustomerDTO>()
    .IncludeMembers(s => s.Customer);

这篇关于具有很多对很多和IncludeMembers的自动映射器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:49