1

Quite new with LINQ. I am wondering how I would be able to achieve this.

I have the following table classes defined:

public partial class Cars
{
    public long ID { get; set; }
    public string CarName { get; set; }
    public long CarModelID { get; set; }

    public virtual CarModel CarModel { get; set; }
}

public partial class CarModel
{
    public long ID { get; set; }
    public string ModelName { get; set; }
    public long StockID { get; set; }
}

public partial class Stock
{
    public long ID { get; set; }
    public string StockName { get; set; }
}

There's also a defined extension for the class Cars (Cars.extension.cs):

public partial class Cars
{
    public List<Stock> StockList { get; set; }
}

I am trying to get all the Cars, CarModel and (List of) Stocks via the following query:

var query = (from cars in Context.Cars.Include("CarModel").Include("StockList")
                         select cars).FirstOrDefault();

It is giving me an error:

"A Specified Include Path is not Valid. The Entity Type Cars does not declare a Navigation Property with the name 'StockList'"

How would I be constructing my LINQ query such it would include possibly the list of Stocks based on a CarModel based off Cars?

1 Answer 1

2

The Include method is adhering to FluentAPI principles, that means further Include() calls are still in the context of the parent entity (Cars) and not in the previously included CarModel.

What you need is:

Cars.Include("CarModel.StockList")

Or

Cars.Include(x => x.CarModel.StockList)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.