Navigation property is not getting mapped

Can someone pls explain why this is happening. I’m trying to get Orders from in memory database but Customer is always null

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>private async Task<IList<RepositoryOrder>> 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;
}
</code>
<code>private async Task<IList<RepositoryOrder>> 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; } </code>
private async Task<IList<RepositoryOrder>> 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;
    }

This is Order model:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
}
</code>
<code>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 } </code>
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

}

And this is Customer model:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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; }
}
</code>
<code>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; } } </code>
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; }
}

This is how i configured relationship:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>modelBuilder.Entity<Customer>(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<Order>(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
</code>
<code>modelBuilder.Entity<Customer>(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<Order>(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 </code>
modelBuilder.Entity<Customer>(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<Order>(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

It seems it correctly queries and selects c.CustomerID (which is CustomerCode in my code) and c.CompanyName but fails to map it:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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,
},
</code>
<code>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, }, </code>
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,
            },

Customer must be not null and in database there is no Order that has null CustomerID

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật