0

I have 3 entities

 public class Sale
{
    public Sale()
    {
        SalesDetails = new List<SalesDetail>();
    }
    [Key]
    public int SaleId { get; set; }

    public DateTime DateAdded { get; set; }
    public decimal Total { get; set; }
    public decimal Discount { get; set; }
    public decimal FinalTotal { get; set; }
    public virtual ICollection<SalesDetail> SalesDetails { get; set; }
}
 public class SalesDetail
{

    [Key]
    public int SaleDetailsId { get; set; }
    public Product Product { get; set; }
    public int Quantity { get; set; }
    public decimal UnitPrice { get; set; }
    public decimal TotalPrice { get; set; }

    public virtual Sale Sales { get; set; }
}
public class Product
{
    [Key]
    public int ProductId { get; set; }
    public string BarCode { get; set; }
    public string ProductName { get; set; }
    public decimal? Price { get; set; }
    public int Quantity { get; set; }
    public DateTime? DateAdded { get; set; }

    public int CategoryId { get; set; }
}

I using Entity Framework How get the data from 3 entities using include I want to get total from Sale and quantity from SaleDetail and Product Name from Product (with SaleID).

1 Answers1

0

You can use Incluse(string path) method that allow you to include not directly related properties. For example:

var salesWithLoadedSubentities = dbContext
                                  .Sales
                                  .Include("SalesDetails")
                                  .Include("SalesDetails.Products");

Now your collection will contains Sale entities with perloaded details and products.

Also, You can use lazy loading for your context but it's not recommended because it can cause performance issues in the production environment.

Vadim Martynov
  • 8,602
  • 5
  • 31
  • 43