Girish Sakhare
Girish Sakhare

Reputation: 763

Entity Framework Include syntax

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.

Upvotes: 2

Views: 5025

Answers (3)

mohsen
mohsen

Reputation: 1806

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(...)

Upvotes: 4

Dennis Wanyonyi
Dennis Wanyonyi

Reputation: 378

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

Upvotes: 2

Ashish Rajput
Ashish Rajput

Reputation: 1529

Use Strong Type here

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

Upvotes: 0

Related Questions