[EntityFramework] 对 DateTime 类型使用 SQL 服务器时间或者字段默认值

DateTime 类型在 SQL 服务器上如果设置了默认值,在 EntityFramework 添加新行的时候想使用该默认值,则不能对新增加的实体的 DateTime 字段赋值。

但是如果新增加的实体 DateTime 不设置,会出现错误如下:

System.Data.Entity.Infrastructure.DbUpdateException - An error occurred while updating the entries. See the inner exception for details.
System.Data.Entity.Core.UpdateException - An error occurred while updating the entries. See the inner exception for details.
System.Data.SqlClient.SqlException - The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.

解决方案:

定位到edmx文件,选中该 DateTime 类型的字段,在属性窗口中,将 StoreGeneratePattern 从 None 改为 Computed 即可。

转载于:https://www.cnblogs.com/lionetchen/p/3966520.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值