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

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

问题描述

我首先使用实体​​框架代码.使用 LINQ to Entity 我想根据 DateTime 值获取记录.这是我当前的代码:

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();

我做错了什么?

推荐答案

我最近在将 Web 应用程序从 Entity Framework 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(...) 方法.是的,这解决了我的问题.

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.

底线:如果您使用的是实体框架 6,请先删除引用 System.Data.Entity DLL,然后在您的代码中替换 EntityFunctions.TruncateTime(..)System.Data.Entity.DbFunctions.TruncateTime(...).[来自 EntityFramework.dll ]

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天全站免登陆