无法获得EntityFunctions.TruncateTime()工作 [英] Can't get EntityFunctions.TruncateTime() to work

查看:657
本文介绍了无法获得EntityFunctions.TruncateTime()工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Entity Framework Code First。使用LINQ to Entity我想根据DateTime值获取记录。这是我当前的代码:

  ///< summary> 
///检查并查看
///数据库中是否已存在Parsed Game的方法。如果是,则返回True,否则返回False。
///< / summary>
///< param name =context>使用Db上下文< / param>
///< param name =homeTeam>游戏的主队的名称。< / param>
///< param name =awayTeam>游戏团队的名称。< / param>
///< param name =date>游戏日期< / param>
///< return>< / returns>
public bool doesGameAlreadyExist(PContext context,String homeTeam,String awayTeam,String date)
{
string dtObjFormat =dd MMM yyyy;
DateTime dt;
DateTime.TryParseExact(date,dtObjFormat,CultureInfo.InvariantCulture,DateTimeStyles.None,out dt);
var result = context.Games.Where(x => EntityFunctions.TruncateTime(x.Start)== dt.Date).FirstOrDefault();
return result!= null;

}

上述代码会引发以下错误:

  LINQ to Entities不识别方法System.Nullable`1 [System.DateTime] TruncateTime(System.Nullable`1 [System.DateTime ])'方法,并且此方法无法转换为存储表达式。 

我也试过下面的代码,我得到了同样的错误:

  var result = context.Games.Where(x => EntityFunctions.TruncateTime(x.Start)== EntityFunctions.TruncateTime(dt.Date))。 FirstOrDefault(); 

我做错了什么?

解决方案

我最近面临这个问题,当我把我的Web应用程序从Entity框架5升级到Entity Framework 6.然后,我意识到 System.Data.Entity DLL需要从应用程序中完全删除为了使用Entity Framework 6. Entity Framework 6不再是.NET Framework的一部分,因此它是独立于System.Data.Entity dll。为了截断时间,你需要使用EntityFramework.dll中的 System.Data.Entity.DbFunctions.TruncateTime(...)方法。是的,这解决了我的问题。

底部:如果您使用的是Entity Framework 6,请先删除 System.Data.Entity code> DLL,然后在你的代码中,用 System.Data.Entity.DbFunctions.TruncateTime替换 EntityFunctions.TruncateTime(..) (...)。 [From EntityFramework.dll]


I am using Entity Framework Code First. Using LINQ to Entity I want to grab a record based on a DateTime value. Here is my current code:

/// <summary>
/// A method to check and see if the Parsed Game already exists on the
/// database. If Yes, then True is returned, otherwise False is returned.
/// </summary>
/// <param name="context">The Db Context to use</param>
/// <param name="homeTeam">The Name of the Home Team for the Game.</param>
/// <param name="awayTeam">The Name of the Away Team for the Game.</param>        
/// <param name="date">The Date of the Game</param>
/// <returns></returns>
public bool doesGameAlreadyExist(PContext context, String homeTeam, String awayTeam, String date)
{
    string dtObjFormat = "dd MMM yyyy";
    DateTime dt;
    DateTime.TryParseExact(date, dtObjFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
    var result = context.Games.Where(x => EntityFunctions.TruncateTime(x.Start) == dt.Date).FirstOrDefault();
    return result != null;

}

The above code throws the following error:

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

I also tried the following code, and I get the same error:

var result = context.Games.Where(x => EntityFunctions.TruncateTime(x.Start) == EntityFunctions.TruncateTime(dt.Date)).FirstOrDefault();

What am I doing wrong?

解决方案

I faced this problem recently when I upgraded my Web Application from Entity Framework 5 to Entity Framework 6. Then, I realized that System.Data.Entity DLL needs to be removed from the application completely in order to work with Entity Framework 6. Entity Framework 6 is not part of .NET Framework anymore and therefore it is independent of System.Data.Entity dll. In order to Truncate time, you would need to use System.Data.Entity.DbFunctions.TruncateTime(...) method from EntityFramework.dll. Yes, that solved my problem.

Bottom Line : If you are using Entity Framework 6, first REMOVE the reference System.Data.Entity DLL and then, in your code, replace EntityFunctions.TruncateTime(..) with System.Data.Entity.DbFunctions.TruncateTime(...). [ From EntityFramework.dll ]

这篇关于无法获得EntityFunctions.TruncateTime()工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆