LINQ to Entities不支持指定的类型成员'DateTime' [英] The specified type member 'DateTime' is not supported in LINQ to Entities
问题描述
我正在使用连接器的最新开发版本 - 6.3.3 beta通过VS2010中的Entity Framework 4连接到一个mysql数据库。
我有遵循使用TIMESTAMP列的Linq语句 - createdDate - 在查询中。这是代码:
int timeThreshold = 5;
DateTimeOffset cutoffTime = DateTime.Now.AddMinutes(-timeThreshold);
使用(var context = new opusismEntities())
{
var unprocessedMessages = from m in context.messages
其中m.createdDate< = cutoffTime
选择m;
try
{
foreach(var message in unprocessedMessages)
{
int gfff = 5;
}
}
catch(异常e)
{
string exceptionString = e.InnerException.ToString();
}
}
CLR抛出以下异常:
MySql.Data.MySqlClient.MySqlException(0x80004005):命令执行过程中遇到致命错误---> MySql.Data.MySqlClient.MySqlException(0x80004005):在MySql.Data.Types.MySqlDateTime.MySql.Data.Types.IMySqlValue.WriteValue(MySqlPacket数据包,布尔二值,对象值,Int32长度)\r\中无法序列化日期/时间值。 n在MySql.Data.MySqlClient.MySqlParameter.Serialize(MySqlPacket数据包,布尔二进制,MySqlConnectionStringBuilder设置)\r\\\
在MySql.Data.MySqlClient.Statement.SerializeParameter(MySqlParameterCollection参数,MySqlPacket数据包,字符串parmName)\r \\\
在MySql.Data.MySqlClient.Statement.InternalBindParameters(String sql,MySqlParameterCollection参数,MySqlPacket数据包)\r\\\
在MySql.Data.MySqlClient.Statement.BindParameters()\r\ (MySql.Data.MySqlClient.Statement.Execute )\r\\\
在MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior行为)\r\\\
在MySql.Data.Entity.EFMySqlCommand.ExecuteDbDataReader(CommandBehavior行为)\r\\\
在系统。 Data.Common.DbCommand.ExecuteReader(CommandBehavior行为)\r\\\
在System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,CommandBehavior行为)
我试图遵循以下链接的建议:
http:/ /bugs.mysql.com/bug.php?id=52550
通过声明一个DateTime而不是DateTimeOffset:
DateTime cutoffTime = DateTime.Now.AddMinutes(-timeThres保持);
...
var unprocessedMessages = from m in context.messages
其中m.createdDate.DateTime< = cutoffTime
select m;
并使用格式.createdDate.DateTime,但实体框架不喜欢它并返回例外:
LINQ to Entities不支持指定的类型成员DateTime
这被报告为以前版本的NET / Connector中的错误。
希望GA版本6.3.4可以解决这个问题,但是仍然存在于6.3.3 beta。
这是LINQ to Entities issue..it不允许你使用属性/在这个查询中的方法不仅仅是DateTime,而且对于别人也是这样。
获取另一个变量中的值,并在查询中使用该变量,如
DateTime dt = createdDate.DateTime;
或只是删除DateTime ...因为它的价值是一样的...应该摆脱异常。但我不知道你正在尝试实现,虽然...
I am using the latest development version of the connector - 6.3.3 beta to connect to a mysql database via the Entity Framework 4 in VS2010.
I have the following Linq statement which uses a TIMESTAMP column - createdDate - in the query. Here is the code:
int timeThreshold = 5;
DateTimeOffset cutoffTime = DateTime.Now.AddMinutes(-timeThreshold);
using (var context = new opusismEntities())
{
var unprocessedMessages = from m in context.messages
where m.createdDate <= cutoffTime
select m;
try
{
foreach (var message in unprocessedMessages)
{
int gfff = 5;
}
}
catch (Exception e)
{
string exceptionString = e.InnerException.ToString();
}
}
The CLR is throwing the following exception:
"MySql.Data.MySqlClient.MySqlException (0x80004005): Fatal error encountered during command execution. ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to serialize date/time value.\r\n at MySql.Data.Types.MySqlDateTime.MySql.Data.Types.IMySqlValue.WriteValue(MySqlPacket packet, Boolean binary, Object value, Int32 length)\r\n at MySql.Data.MySqlClient.MySqlParameter.Serialize(MySqlPacket packet, Boolean binary, MySqlConnectionStringBuilder settings)\r\n at MySql.Data.MySqlClient.Statement.SerializeParameter(MySqlParameterCollection parameters, MySqlPacket packet, String parmName)\r\n at MySql.Data.MySqlClient.Statement.InternalBindParameters(String sql, MySqlParameterCollection parameters, MySqlPacket packet)\r\n at MySql.Data.MySqlClient.Statement.BindParameters()\r\n at MySql.Data.MySqlClient.Statement.Execute()\r\n at MySql.Data.MySqlClient.PreparableStatement.Execute()\r\n at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)\r\n at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)\r\n at MySql.Data.Entity.EFMySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)\r\n at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)\r\n at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)"
I’ve attempted to follow the advice from the following link: http://bugs.mysql.com/bug.php?id=52550
by declaring a DateTime instead of DateTimeOffset:
DateTime cutoffTime = DateTime.Now.AddMinutes(-timeThreshold);
...
var unprocessedMessages = from m in context.messages
where m.createdDate.DateTime <= cutoffTime
select m;
and use the format .createdDate.DateTime, but Entity Framework doesn’t like it and returns back an exception:
The specified type member 'DateTime' is not supported in LINQ to Entities
This was reported as a bug in previous versions of NET/Connector.
Hopefully the GA version 6.3.4 will fix this issue, but it still persists in 6.3.3 beta.
Well this is LINQ to Entities issue..it does not allow you to use properties/methods like that in a query...not only for DateTime but for others too.
Get the value in another variable and use that variable in the query like
DateTime dt=createdDate.DateTime;
Or just remove DateTime...because it's value is the same...that should get rid of exception.But I am not sure what you are trying achieve though...
这篇关于LINQ to Entities不支持指定的类型成员'DateTime'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!