实体框架未声明datetime.now为null [英] Entity Framework not claiming datetime.now is null

查看:73
本文介绍了实体框架未声明datetime.now为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

UVCUpdate update = new UVCUpdate();
update.CurrentDate = DateTime.Now;
_context.UVCUpdates.Add(update);

_context.SaveChanges();

现在,我说了一个内在的例外:

Now I am getting an inner exception though saying this:


不能将值NULL插入表'bLinked.dbo.BlackbookUpdateUVC'的'CurrentDate'列中;列不允许为空。插入失败。

Cannot insert the value NULL into column 'CurrentDate', table 'bLinked.dbo.BlackbookUpdateUVC'; column does not allow nulls. INSERT fails.

如果我输出DateTime。现在在此代码之前,它输出:

If I output the DateTime.Now just before this code it outputs:


9/15/2016 7:26:35 PM

9/15/2016 7:26:35 PM

我的数据类型db中的CurrentDate设置为datetime,在类中将其设置为DateTime。

My data type for CurrentDate in the db is set to datetime and in the class it is set to DateTime. Neither allow for nulls, but DateTime.Now should not be null right?

推荐答案

这几乎总是会在两者之间不匹配时发生在EF模型和数据库之间称为存储生成模式。如果模型列具有存储的标识或计算的生成模式-这意味着EF将确保数据库在插入或更新时将自动提供这些值,并且无需在INSERT或UPDATE语句中包括它们。缺少的值将具有默认的NULL值,并且如果此列同时在数据库中不可为空,并且不是真正的计算值或标识-您有问题的错误。

It almost always happens when there is mismatch between so called "store generated pattern" between EF model and database. If model column has store generated pattern of Identity or Computed - that means EF will be sure those values will be automatically provided by database on insert or update, and there is no need to include them in INSERT or UPDATE statements. Missing values will have default NULL value, and if this column is non-nullable in database at the same time, and is not really computed or identity - you have the error in question.

这篇关于实体框架未声明datetime.now为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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