Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

c# - Canonical Function "EntityFunctions.TruncateTime" does not exist in MYSQL

I'm trying to run this query:

DateTime DDate=DateTime.Today; //Today's date without Time
var v= db.measurements.Where(m => EntityFunctions.TruncateTime(m.InDate) == DDate);

It just returns objects where those two dates are equal, ignoring the time part.

But I receive:

{"FUNCTION [database].TruncateTime does not exist"}

StackTrace:

at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.Entity.EFMySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)

I'm using:

  • C# Visual Studio 2010
  • EntityFramework 4 database first
  • NetFramework 4
  • MYSQL Server 5.6

The version of MySQL.Data and MySQL.Data.Entity is 6.6.5.0

TruncateTime is supported by MySQL.

Same thing happened to this person. See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I couldn't resolve it, so I just created a Function named "TruncateTime" in the database.

Create FUNCTION TruncateTime(dateValue DateTime) RETURNS date
return Date(dateValue);

And it works, but I don't like it.

These people did similar things:

Alternative to EntityFunctions.AddSeconds for MySQL

CurrentUtcDateTime does not exist - Entity Framework and MySql

So now I think that might be unnecessary and I can just call it directly from the database and still get entities, something like this:

var x = db.ExecuteStoreQuery<Measurement>(@"SELECT field1,field2
FROM   Measurements
WHERE  Date(InDate) = {0}", DDate);

And that's all.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...