正确配置多对一关系 ef core
Configuring Many-to-One relationship correctly ef core
我在我的项目中使用 CQRS 模式。我希望我的客户实体可能有一个地址数组,因此我配置了一对多 EF 关系。但是,当我创建一个新客户然后检索它时,地址字段只是一个空数组:
{
"id": 1,
"firstName": "string",
"lastName": "string",
"phone": "string",
"email": "string",
"primaryContact": "string",
"secondaryContact": "string",
"addresses": [],
"orders": []
}
我对使用 AutoMapper 还很陌生,所以我想知道我是否没有正确配置关系
客户实体:
public class Customer : AuditableEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string PrimaryContact { get; set; }
public string SecondaryContact { get; set; }
#region Navigation Properties
public virtual ICollection<Address> Addresses { get; set; }
public virtual ICollection<Order> Orders { get; set; }
#endregion
}
地址实体:
public class Address : AuditableEntity
{
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
}
AddressDTO 只是在地址实体中设置的属性,没有导航属性。
public class AddressCreateDto
{
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
}
GetCustomerByIdResponse
public class GetCustomerByIdResponse
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string PrimaryContact { get; set; }
public string SecondaryContact { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
我的 Auto Mapper 个人资料:
public AddressProfile()
{
CreateMap<AddressCreateDto, Address>().ReverseMap();
CreateMap<CreateCustomerCommand, Address>().ReverseMap();
}
public CustomerProfile()
{
CreateMap<CreateCustomerCommand, Customer>().ReverseMap();
CreateMap<GetCustomerByIdResponse, Customer>()
.ForMember(dst => dst.Orders, opt => opt.MapFrom(src =>
src.Orders))
.ReverseMap();
}
像您一样从预编辑的 post 修复关系后,我会检查并确保在使用 EF 检索 Customer
实体时您使用的是
Include(x => x.Addresses)
所以它看起来有点像这样(简化版)
var customers = Customers
.Include(x => x.Addresses)
.Include(x => x.Orders)
.ToList();
否则 EF 核心不会从数据库中获取它们。 Orders
也是如此,他们需要单独的 Include
声明。
我在我的项目中使用 CQRS 模式。我希望我的客户实体可能有一个地址数组,因此我配置了一对多 EF 关系。但是,当我创建一个新客户然后检索它时,地址字段只是一个空数组:
{
"id": 1,
"firstName": "string",
"lastName": "string",
"phone": "string",
"email": "string",
"primaryContact": "string",
"secondaryContact": "string",
"addresses": [],
"orders": []
}
我对使用 AutoMapper 还很陌生,所以我想知道我是否没有正确配置关系
客户实体:
public class Customer : AuditableEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string PrimaryContact { get; set; }
public string SecondaryContact { get; set; }
#region Navigation Properties
public virtual ICollection<Address> Addresses { get; set; }
public virtual ICollection<Order> Orders { get; set; }
#endregion
}
地址实体:
public class Address : AuditableEntity
{
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
}
AddressDTO 只是在地址实体中设置的属性,没有导航属性。
public class AddressCreateDto
{
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
}
GetCustomerByIdResponse
public class GetCustomerByIdResponse
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string PrimaryContact { get; set; }
public string SecondaryContact { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
我的 Auto Mapper 个人资料:
public AddressProfile()
{
CreateMap<AddressCreateDto, Address>().ReverseMap();
CreateMap<CreateCustomerCommand, Address>().ReverseMap();
}
public CustomerProfile()
{
CreateMap<CreateCustomerCommand, Customer>().ReverseMap();
CreateMap<GetCustomerByIdResponse, Customer>()
.ForMember(dst => dst.Orders, opt => opt.MapFrom(src =>
src.Orders))
.ReverseMap();
}
像您一样从预编辑的 post 修复关系后,我会检查并确保在使用 EF 检索 Customer
实体时您使用的是
Include(x => x.Addresses)
所以它看起来有点像这样(简化版)
var customers = Customers
.Include(x => x.Addresses)
.Include(x => x.Orders)
.ToList();
否则 EF 核心不会从数据库中获取它们。 Orders
也是如此,他们需要单独的 Include
声明。