使用包含在实体框架4中的 lambda 表达式

我看过很多关于如何克服这个问题的文章,都与 CTP4有关,或者添加我自己的扩展方法。

是否有一个“官方的”EF4包含的方式来使用 lambda 表达式内包括(对于第一级关系,也是第二级和更多级)或它最终没有包括在 RTM?

它有一个-我很高兴学习如何做到这一点,因为现在在我的代码中使用 lambda 表达式(使用 # system)。实体 # 系统。Data.linq)仍然给我:

无法将 lambda 表达式转换为“ string”类型,因为它不是委托类型 关于:

var customers = from c in
context.Customers.Include(c=>c.Phone)
86009 次浏览

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.

The RTM version of Entity Framework 4.1 actually includes extension methods in the EntityFramework.dll file, for eager loading with lambda through the Include function. Just include the DLL in your project and you should be able to write code like:

var princesses1 = context.Princesses.Include(p => p.Unicorns).ToList();

Remember to add an Import/Using statement to include the System.Data.Entity namespace. Otherwise the compiler cannot find the extension methods. E.g:

using System.Data.Entity;

See this ADO.NET team blog article for more information.

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:

using System.Data.Entity;