0

I have an order entity, inside it contains several entities Customer, Store and others, but Entity Framework does not fill those entities. I thought the relationships were wrong but I can't find why the entity framework does not map the entities within orders.

Orders and Customers entities:

public partial class Orders
{
    public Orders()
    {
        OrderItems = new HashSet<OrderItems>();
    }

    public int OrderId { get; set; }
    public int? CustomerId { get; set; }
    public byte OrderStatus { get; set; }
    public DateTime OrderDate { get; set; }
    public DateTime RequiredDate { get; set; }
    public DateTime? ShippedDate { get; set; }
    public int StoreId { get; set; }
    public int StaffId { get; set; }

    public virtual Customers Customer { get; set; }
    public virtual Staffs Staff { get; set; }
    public virtual Stores Store { get; set; }
    public virtual ICollection<OrderItems> OrderItems { get; set; }
}

public partial class Customers
{
    public Customers()
    {
        Orders = new HashSet<Orders>();
    }

    public int CustomerId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }

    public virtual ICollection<Orders> Orders { get; set; }
}

Entity Framework code fragment:

        modelBuilder.Entity<Orders>(entity =>
        {
            entity.HasKey(e => e.OrderId)
                .HasName("PK__orders__46596229F9C56686");

            entity.ToTable("orders", "sales");

            entity.Property(e => e.OrderId).HasColumnName("order_id");

            entity.Property(e => e.CustomerId).HasColumnName("customer_id");

            entity.Property(e => e.OrderDate)
                .HasColumnName("order_date")
                .HasColumnType("date");

            entity.Property(e => e.OrderStatus).HasColumnName("order_status");

            entity.Property(e => e.RequiredDate)
                .HasColumnName("required_date")
                .HasColumnType("date");

            entity.Property(e => e.ShippedDate)
                .HasColumnName("shipped_date")
                .HasColumnType("date");

            entity.Property(e => e.StaffId).HasColumnName("staff_id");

            entity.Property(e => e.StoreId).HasColumnName("store_id");

            entity.HasOne(d => d.Customer)
                .WithMany(p => p.Orders)
                .HasForeignKey(d => d.CustomerId)
                .OnDelete(DeleteBehavior.Cascade)
                .HasConstraintName("FK__orders__customer__0F4872E8");

            entity.HasOne(d => d.Staff)
                .WithMany(p => p.Orders)
                .HasForeignKey(d => d.StaffId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK__orders__staff_id__1130BB5A");

            entity.HasOne(d => d.Store)
                .WithMany(p => p.Orders)
                .HasForeignKey(d => d.StoreId)
                .HasConstraintName("FK__orders__store_id__103C9721");
        });

Controller code:

enter image description here

Response in Postman:

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Aetos2501
  • 165
  • 1
  • 9

1 Answers1

3

One of the options is to eager load them via Include:

return await _context.Orders
    .Include(o => o.Customer)
    .Include(o => o.Staff)
    .Include(o => o.Store)
    .ToListAsync()

For more info and options please check out the "Loading Related Data" doc.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • It works when I debug the API, but when I make the request (POSTMAN - GET) I get a "500 Internal Server Error". The VS2019 debugging console throws me the following "A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32". – Aetos2501 Jun 10 '21 at 23:59
  • 1
    @Aetos2501 please check out [this](https://stackoverflow.com/a/62985944/2501279) answer – Guru Stron Jun 11 '21 at 00:10