Код: Выделить всё
private async Task GetOrdersInternalAsync(int skip, int count)
{
var repositoryOrders = await this.context.Orders
.Include(x => x.Customer)
.Include(x => x.Employee)
.Include(x => x.Shipper)
.Include(x => x.OrderDetails).ThenInclude(x => x.Product)
.Skip(skip)
.Take(count)
.OrderBy(x => x.Id)
.Select(x => OrderMapper.Map(x))
.ToListAsync();
return repositoryOrders;
}
Код: Выделить всё
public class Order
{
public Order(long id, Customer customer, // other properties)
{
}
private Order()
{
}
public long Id { get; }
public virtual Customer Customer { get; set; } = null!;
// other properties
}
Код: Выделить всё
public class Customer
{
public Customer(CustomerCode code, string companyName)
{
this.CustomerCode = code;
this.CompanyName = companyName;
}
private Customer()
{
}
public CustomerCode CustomerCode { get; }
public string CompanyName { get; }
}
public class CustomerCode
{
public CustomerCode(string code)
{
this.Code = code;
}
public string Code { get; init; }
}
Код: Выделить всё
modelBuilder.Entity(opt =>
{
opt.ToTable("Customers");
opt.Property(x => x.CustomerCode)
.HasConversion(x => x.Code, y => new CustomerCode(y))
.HasColumnName("CustomerID")
.IsRequired();
opt.HasKey(x => x.CustomerCode);
opt.Property(x => x.CompanyName).HasMaxLength(255);
});
modelBuilder.Entity(opt =>
{
opt.ToTable("Orders");
opt.HasKey(x => x.Id).HasName("OrderID");
opt.Property(x => x.Id).HasColumnName("OrderID");
opt.HasOne(x => x.Customer).WithMany().HasForeignKey("CustomerID");
// other unrelated properties
Код: Выделить всё
public static RepositoryOrder Map(Order order)
{
var repositoryOrder = new RepositoryOrder(order.Id)
{
Customer = new RepositoryCustomer(new RepositoryCustomerCode(order.Customer.CustomerCode.Code)) // this is null here and throwing exception
{
CompanyName = order.Customer.CompanyName,
},
Подробнее здесь: https://stackoverflow.com/questions/784 ... ing-mapped