实体框架错误:datetime2数据类型到datetime数据的转换 [英] Entity framework error: The conversion of a datetime2 data type to a datetime data

查看:97
本文介绍了实体框架错误:datetime2数据类型到datetime数据的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有关此问题的帖子很多,但似乎没有一个可以解决我的问题。



在这种情况下:



我有一个 CreateDate DateTime <我的MS SQL Server数据库用户表中的/ em>列是不可为空的,并使用默认值或绑定设置中的 GetDate()方法自动设置。 / p>

我可以使用标准EF插入创建用户,但是当我尝试更新用户时,出现此错误:


将datetime2数据
类型转换为datetime数据类型会导致
超出范围。


不让EF担心CreateDate列进行更新的诀窍是什么?我有StoreGenerationPattern = Identity,但这无济于事。



以下是我的Entity属性的EF属性:
http://screencast.com/t/8ndQRn9N



这是我的更新方法:
http://screencast.com/t/UXIzhkhR

解决方案

您是否尝试过将StoreGeneratedPattern属性设置为 Computed。否则,请尝试将列的数据类型更改为 datetime2



[出现问题是因为.NET DateTime.MinValue 等于0001-1-1,而Sql Server DateTime 涵盖了1753-1-1至9999-12-31]


I know there are a ton of posts about this issue but none of them seem to solve my problem.

Here's the scenario:

I have a CreateDate DateTime column in my MS SQL Server database User table that is non-nullable and is automatically set using GetDate() method in "Default Value or Binding" setting.

I am able to create a User just fine with the standard EF Insert but when I try to update the user, I get this error:

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.

What is the trick to not having the EF worry about the CreateDate column for updates? I have the StoreGenerationPattern = Identity but that isn't helping.

Here are the EF properties for my Entity Property: http://screencast.com/t/8ndQRn9N

And here is my Update method: http://screencast.com/t/UXIzhkhR

解决方案

Have you tried setting the StoreGeneratedPattern attribute to 'Computed'. Failing that, as a last resort, try changing the column's datatype to datetime2.

[The problem arises because the .NET DateTime.MinValue equals 0001-1-1 but Sql Server DateTime covers 1753-1-1 through 9999-12-31]

这篇关于实体框架错误:datetime2数据类型到datetime数据的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆