在 Entity Framework 4 中使用 Include 和 lambda 表达式
我看过很多关于如何克服这个问题的文章,都与CTP4有关,或者添加我自己的扩展方法。
是否有一种“官方”EF4 包含方法可以在包含内使用 lambda 表达式(对于第一级关系以及第二级和更高级别),或者它最终没有包含在 RTM 中?
它有一个 - 我很高兴学习如何做到这一点,因为现在在我的代码中使用 lambda 表达式(使用 #system.data.entity #system.data.linq)仍然给我:
无法将 lambda 表达式转换为类型 ' string' 因为它不是委托类型 在:
var customers = from c in
context.Customers.Include(c=>c.Phone)
I've seen many articles about how to overcome this matter, all related to CTP4, Or adding my own extension methods.
Is there an "official" EF4 included way to use lambda expressions inside include (for both first level relations and also 2nd and more level) or is it eventually was not included in the RTM ?
It there is one - I would be glad to learn how to do it, as using lambda expression in my code now (with #system.data.entity #system.data.linq) still gives me:
Cannot convert lambda expression to type 'string' because it is not a delegate type
on:
var customers = from c in
context.Customers.Include(c=>c.Phone)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Entity Framework 4.1 的 RTM 版本实际上在
EntityFramework.dll
文件中包含扩展方法,以便通过Include
函数预先加载 lambda。只需将 DLL 包含在您的项目中,您就应该能够编写如下代码:请记住添加一个 Import/Using 语句以包含 System.Data.Entity 命名空间。否则编译器无法找到扩展方法。例如:
请参阅此 ADO.NET 团队博客文章了解更多信息。
The RTM version of Entity Framework 4.1 actually includes extension methods in the
EntityFramework.dll
file, for eager loading with lambda through theInclude
function. Just include the DLL in your project and you should be able to write code like:Remember to add an Import/Using statement to include the System.Data.Entity namespace. Otherwise the compiler cannot find the extension methods. E.g:
See this ADO.NET team blog article for more information.
尽管问题中隐含了这一点,但对于遇到无法将 lambda 与 .Include 一起使用的相同问题的其他人,请确保您拥有以下内容:
Although this is implied in the question, for anyone else who has the same problem where they can't use lambdas with .Include, make sure you have this:
否,目前 RTM 中没有对包含 lambda 表达式的官方支持。我正在使用 这个。
当我们谈论 CTP4 时,我们指的是实体框架功能。它是比 EF4 更新的 API。它主要包括 Code First 和其他一些改进。
No there is no official support for Include with lambda expression in RTM at the moment. I'm using this.
When we are talking about CTP4 we are meaning Entity Framework Feature. It is newer API than EF4. It mainly includes Code First and few other improvements.