EF 更新异常:datetim2 数据类型到日期时间的转换
我将一些小的 XML 文件解析为 EF 对象,然后将它们加载到 SQL Server 2008 数据库中。
表定义:
CREATE TABLE [dbo].[Events](
[ID] IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL,
[EventDate] datetime NULL
)
XML:
<root>
<event date="2012-02-16T13:14:53" />
<event date="2012-02-16T13:14:53" />
<event date="2012-02-15T18:48:32" />
<root>
解析 XML:
var events = XDocument.Load(xmlStream)
.Descendants("event")
.Select(x => new Event
{
EventDate = (DateTime?)x.Attribute("date")
});
推送到数据库:
foreach(var event in events)
{
dbContext.Events.Add(event);
}
dbContext.SaveChanges();
我收到以下错误:
System.Data.SqlClient.SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
我四处搜索并尝试了以下所有方法均无济于事:
- 将 EDMX 'ProviderManifestToken' 从“2008”更改为“2005
- ”将“StoreGeneratePattern”更改为“计算”
- 将 Linq 更改为 XML 表达式
似乎没有任何效果...这里没有想法...
I'm parsing some small XML files into EF objects, then loading them into a SQL Server 2008 database.
Table definition:
CREATE TABLE [dbo].[Events](
[ID] IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL,
[EventDate] datetime NULL
)
XML:
<root>
<event date="2012-02-16T13:14:53" />
<event date="2012-02-16T13:14:53" />
<event date="2012-02-15T18:48:32" />
<root>
Parsing the XML:
var events = XDocument.Load(xmlStream)
.Descendants("event")
.Select(x => new Event
{
EventDate = (DateTime?)x.Attribute("date")
});
Pushing to DB:
foreach(var event in events)
{
dbContext.Events.Add(event);
}
dbContext.SaveChanges();
And I get the following error:
System.Data.SqlClient.SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
I've searched around and tried all of the following to no avail:
- Changing the EDMX 'ProviderManifestToken' from "2008" to "2005"
- Changing the 'StoreGeneratedPattern' to "Computed"
- Altering the Linq to XML expression
Nothing seems to be working... running out of ideas here...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该解决方案与解析该特定日期时间无关。我忘记了数据库中有一个使用 DEFAULT 约束的日期时间列。一旦我解决了这个问题,一切就都正常了。
The solution had nothing to do with parsing that particular DateTime. I forgot I had a datetime column in the db that used a DEFAULT constraint. Once I took care of this, everything was working fine.
如果我从 XML 字符串解析 DateTime,我会尝试使用 DateTime.Parse
看起来您的日期时间可以通过简单的 String.replace 进行解析。
我认为有一个解析函数,您可以将格式化字符串传递给它(例如
"yyyy-MM-ddThh:mm:ss"
),但我现在似乎找不到文档。If I were parsing a DateTime from an XML string, I would try to use
DateTime.Parse
It looks like that your datetime can be made parsable with a simple
String.replace
.I thought there was a parse function that you could pass a formatting string to(like
"yyyy-MM-ddThh:mm:ss"
), but I can't seem to find the documentation right now.如前所述,尝试直接解析日期。
当尝试将 DateTime.MinValue 或 DateTime.MaxValue 插入 sql 时,通常会出现此类错误。这不是一个好主意。可以打印事件吗?
As mentioned try parsing the date directly.
This type of errors usually appear when trying to insert DateTime.MinValue or DateTime.MaxValue to sql. Which is not a good idea. Can you print out events?