简体   繁体   中英

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.

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

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

在这里使用强类型

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM