2

We are using Include in few of our LINQ queries. However was wondering that the include method overload only shows (string path) as parameter, what if we change the database table name(s) and regenerate the entities then the include part will throw runtime errors. How to catch such issues at compile time?

Example:

Material has BusinessUnit. 
So we use repo.Material.Include("BusinessUnit")

what if we change BusinessUnit entity name to OrgUnit or something else.

1
  • 1
    That feature is already existing in Entity Framework 4.1+. See reference here Commented May 27, 2016 at 6:28

3 Answers 3

4

Use like below to avoid using string in Include method

Add the following reference to your file

using System.Data.Entity ;

And use

Context.BusinessUnits.Load();

Or

Context.Materials.Include(m => m.BusinessUnit).Where(...)
Sign up to request clarification or add additional context in comments.

Comments

2

In order to use to overload of Include that uses a lambda expression, then you need to add using System.Data.Entity; namespace. The overload is contained in that namespace. Then you would be able to use

repo.Material.Include(m => m.BusinessUnit)

You have to be using Entity Framework 4.1 or later to use utilize this functionality

Comments

0

Use Strong Type here

repo.Material.Include(m => m.BusinessUnit)

2 Comments

I am using entity framework 6.0, somehow I am not getting that overload. Am I missing something here? If I hit F12 on include i just see public virtual DbQuery<TResult> Include(string path); Do I need to write overload myself?
this is available from ef 4.1. Not sure why you are not getting the option. please share your entity schema

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.