实体框架包含语法

时间:2016-05-27 06:23:24

标签: c# entity-framework linq

我们在少数LINQ查询中使用Include。但是想知道include方法重载只显示(字符串路径)作为参数,如果我们更改数据库表名并重新生成实体,那么include部分将抛出运行时错误。如何在编译时捕获这些问题?

示例:

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

如果我们将BusinessUnit实体名称更改为OrgUnit或其他内容,该怎么办。

3 个答案:

答案 0 :(得分:2)

使用以下内容避免在string方法

中使用Include

将以下引用添加到您的文件

using System.Data.Entity ;

并使用

Context.BusinessUnits.Load();

Context.Materials.Include(m => m.BusinessUnit).Where(...)

答案 1 :(得分:1)

为了使用使用lambda表达式的Include的重载,您需要添加using System.Data.Entity;命名空间。重载包含在该命名空间中。然后你就可以使用

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

您必须使用Entity Framework 4.1或更高版本才能使用此功能

答案 2 :(得分:0)

在这里使用强类型

{{1}}