是否有一个简单的方法来使用的EntityFramework SQL默认值? [英] Is there an easy way to make EntityFramework use SQL default values?

查看:190
本文介绍了是否有一个简单的方法来使用的EntityFramework SQL默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我的大部分实体有dateCreated会和DateModified领域。默认值这些在SQL Server设置为GETUTCDATE()。

For example, most of my entities have DateCreated and DateModified fields. The defaults to these are set to GetUtcDate() in SQL Server.

如果我试图创建一个实体,并且不设置这些值,我得到一个异常说它由于日期值超出范围不能运行SQL插入。有意义的,因为C#默认的日期是1/1/0001,而SQL Server的最小日期为1753年1月1日。

If I try and create an entity and don't set these values, I get an exception saying it can't run the SQL insert because the date value is out of range. Makes sense because C# default dates are 1/1/0001 while SQL Server's minimum date is 1/1/1753.

那么,有没有办法,我可以告诉EF要么使用SQL Server的默认值,或者不要试图插入其中尚未设置列?

So is there a way I can tell EF to either use the SQL Server default values, or to NOT try and insert columns which have not been set?

推荐答案

您必须设置 StoreGeneratedPattern 这些属性为身份 dateCreated会已计算 DataModified 。这是设计师可用。一旦你这样做,你不能在你的应用程序修改的价值观 - 只有数据库可以设置这些属性。我写这个一些文章,因为此功能有VS2010 SP1之前的错误,但有报道称,它仍然无法正常工作,有时

You must set StoreGeneratedPattern for those properties to Identity for DateCreated and Computed for DataModified. It is available in designer. Once you do that you cannot modify those values in your application - only database can set those properties. I wrote about this some article because this feature had bug before VS2010 SP1 but there are reports that it still doesn't work sometimes.

这篇关于是否有一个简单的方法来使用的EntityFramework SQL默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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