我有一个具有以下实体类的ASP.NET Core应用程序:
public class CustomerEntity : IAuditableEntity {
private readonly List<AddressEntity> _addresses = new List<AddressEntity>();
public CustomerEntity() {
Id = default;
IsPremierCustomer = false;
}
public int Id { get; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int CustomerTypeId { get; set; }
public string EmailAddress { get; set; }
public bool IsPremierCustomer { get; private set; }
public CustomerTypeEntity CustomerType { get; }
public IReadOnlyCollection<AddressEntity> Addresses => _addresses;
public void AddAddress(AddressEntity address) {
_addresses.Add(address);
}
public void SetPremierStatus(bool premierCustomer) {
if (IsPremierCustomer == premierCustomer) {
return;
}
IsPremierCustomer = premierCustomer;
}
}
public class CustomerTypeEntity {
public CustomerTypeEntity(string description) {
Id = default;
Description = description;
}
public int Id { get; }
public string Description { get; }
}
public class AddressEntity : IAuditableEntity {
public AddressEntity() {
Id = default;
}
public int Id { get; }
public int CustomerId { get; set; }
public int AddressTypeId { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Line3 { get; set; }
public string Province { get; set; }
public string PostalCode { get; set; }
public int CountryId { get; set; }
public AddressTypeEntity AddressType { get; }
public CountryEntity Country { get; }
}
public class AddressTypeEntity {
public AddressTypeEntity(string description) {
Id = default;
Description = description;
}
public int Id { get; }
public string Description { get; }
}
public class CountryEntity {
public CountryEntity(int numericCode, string alpha2Code, string alpha3Code, string englishName, string frenchName) {
NumericCode = numericCode;
Alpha2Code = alpha2Code;
Alpha3Code = alpha3Code;
EnglishName = englishName;
FrenchName = frenchName;
}
public int NumericCode { get; }
public string Alpha2Code { get; }
public string Alpha3Code { get; }
public string EnglishName { get; }
public string FrenchName { get; }
}
以下是相应的DTO类:
public class CustomerWithAddressDTO {
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string CustomerType { get; set; }
public string EmailAddress { get; set; }
public bool IsPremierCustomer { get; set; }
public IEnumerable<AddressDTO> Addresses { get; set; }
}
public class AddressDTO {
public int Id { get; set; }
public string AddressType { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Line3 { get; set; }
public string Province { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
}
我的映射定义如下:
public class CustomerWithAddressProfile : Profile {
public CustomerWithAddressProfile() {
CreateMap<AddressEntity, AddressDTO>()
.ForMember(dest => dest.AddressType, src => src.MapFrom(x => x.AddressType.Description))
.ForMember(dest => dest.Country, src => src.MapFrom(x => x.Country.Alpha3Code));
CreateMap<CustomerEntity, CustomerWithAddressDTO>()
.ForMember(dest => dest.CustomerType, src => src.MapFrom(x => x.CustomerType.Description))
.ForMember(dest => dest.Addresses, src => src.MapFrom(x => x.Addresses));
}
}
在我的代码中,我从数据库和地图中获取客户,包括地址,如下所示:
[HttpGet]
public ActionResult<IEnumerable<CustomerWithAddressDTO>> GetAllCustomers() {
var customers = _customerWithAddressRepository.FindAll();
var customerDTOs = _mapper.Map<ICollection<CustomerEntity>, IEnumerable<CustomerDTO>>(customers);
return Ok(customerDTOs);
}
正在正确填充(映射)客户。但是嵌套地址永远不会被填充。我一直在寻找解决方案,但仍然没有运气。有谁有任何想法。
谢谢。