Can't get EntityFunctions.TruncateTime() to work

末鹿安然 提交于 2019-11-26 19:58:07

问题


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?


回答1:


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 ]




回答2:


I didn't remove the reference System.Data.Entity, I just changed the call from DbFunctions.TruncateTime to System.Data.Entity.DbFunctions.TruncateTime and it worked for me.

I'm using Entity 6.




回答3:


DateTime? dt = DateTime.Parse(sText);
campaigns = _CampaignMasterRepository.Get().OrderByDescending(a => a.LaunchOn).Where(a => a.UserId == obj_User.ID && a.CampaignStatus == (int)OmevoEnums.CampaignStatus.Sent && a.CampaignName.Contains(sText) || DbFunctions.TruncateTime(a.LaunchOn) == DbFunctions.TruncateTime(dt)).ToList();

For getting the date results through Linq queries use this function.



来源:https://stackoverflow.com/questions/16217016/cant-get-entityfunctions-truncatetime-to-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!