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.

Girish Sakhare
  • 743
  • 7
  • 14

3 Answers3

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(...)
mohsen
  • 1,763
  • 3
  • 17
  • 55
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

Dennis Wanyonyi
  • 368
  • 1
  • 5
  • 18
0

Use Strong Type here

repo.Material.Include(m => m.BusinessUnit)
Ashish Rajput
  • 1,489
  • 1
  • 11
  • 20
  • 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 Include(string path); Do I need to write overload myself? – Girish Sakhare May 27 '16 at 06:37
  • this is available from ef 4.1. Not sure why you are not getting the option. please share your entity schema – Ashish Rajput May 27 '16 at 07:04