Hooman Hooman - 3 years ago 56
C# Question

Loading related entities in EF6

I have 3 tables which are related to each others:

has many

has many

so i want to retrieve Suggested Prices like so :

await ctx.SuggestedPrices
.OrderByDescending(pp => pp.SuggestionDate)
.Include(p1 => p1.Customer)
.Include(p2 => p2.Product)
.Include(p3 => p3.Product.ProductPricing)

According to your request: i added the followings:

Product class :

public class Product : ReportingBase {
// Product-ProductPricing -> One Product has many Prices
public virtual ICollection<ProductPricing> ProductPricing { get; set; }
// Product-SuggestedPrices
public virtual ICollection<SuggestedPrice> SuggestedPrices { get; set; }

ProductPricing class :

public class ProductPricing {
// ProductPricings-Product
public virtual Product Product { get; set; }
public int ProductId { get; set; }

SuggestedPrice class:

public class SuggestedPrice : EntityBase {
// SuggestedPrices-Product
public virtual Product Product { get; set; }
public int ProductId { get; set; }

// SuggestedPrices-Customer
public virtual ApplicationUser Customer { get; set; }
public string CustomerId { get; set; }

but when i add line
.Include(p3 => p3.Product.ProductPricing)
i get an error which says:

Use dotted paths for reference navigation properties and the Select operator for collection navigation properties

how do i get rid of this error?

thank to your answers

Answer Source

After some struggling I find out that there's no need to include Product.ProductPricing and it's already there:

OrgPrice = p.Product.ProductPricing.FirstOrDefault().OrgPrice,
PriceAfterDiscount = p.Product.ProductPricing.FirstOrDefault().Price
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download