具有多对多和 IncludeMembers 的自动映射器
Automapper with many to many and IncludeMembers
下面是我的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>();
}
自动映射器配置基于此 link
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 允许这样做,但需要您为集合元素类型创建映射 - 在这种特定情况下,AccountCustomer
到 CustomerDTO
(在链接的 post 中有类似的映射BookCategory
到 CategoryDto
).
因此,您至少需要在映射器配置中添加如下内容:
CreateMap<AccountCustomer, CustomerDTO>()
.IncludeMembers(s => s.Customer);
下面是我的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>();
}
自动映射器配置基于此 link
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 允许这样做,但需要您为集合元素类型创建映射 - 在这种特定情况下,AccountCustomer
到 CustomerDTO
(在链接的 post 中有类似的映射BookCategory
到 CategoryDto
).
因此,您至少需要在映射器配置中添加如下内容:
CreateMap<AccountCustomer, CustomerDTO>()
.IncludeMembers(s => s.Customer);