Linq中的规范函数对实体不起作用

时间:2014-04-28 19:31:35

标签: c# sql linq

尝试在我的代码中运行此查询:

var rslt = ent.AppointmentDiaries.Where(s => s.DateTimeScheduled >=
                fromDate && EntityFunctions.AddMinutes(
                s.DateTimeScheduled, s.AppointmentLength) <= toDate);

它一直与:

分手
An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Additional information: LINQ to Entities does not recognize the method 'System.Nullable`1[System.DateTime] AddMinutes(System.Nullable`1[System.DateTime], System.Nullable`1[System.Int32])' method, and this method cannot be translated into a store expression.

使用entityframework 6.1.0 ...

我知道规范的连接,所以我希望EntityFunctions.AddMinutes能够处理codefirst查询......

知道我做错了吗?

2 个答案:

答案 0 :(得分:6)

在实体框架6中,EntityFunctions已被DbFunctions取代。旧班级现已标记为过时,将来会消失。

我必须承认,当谷歌发布发行说明时,这并不是直接明显的。然而,有一个博客描述如何 upgrade to EF6提到了这种变化。

答案 1 :(得分:1)

是的,就像@Gert说的那样。这是代码:

var rslt = ent.tbl_Appoiment_Diary.Where(s => s.DateTimeScheduled >= fromDate && DbFunctions.AddMinutes(s.DateTimeScheduled, s.AppointmentLenght) <= toDate);

希望这会有所帮助。感谢@Gert!干杯。