可能默认DateTime字段到GETDATE()与实体框架迁移? [英] Possible to default DateTime field to GETDATE() with Entity Framework Migrations?

查看:115
本文介绍了可能默认DateTime字段到GETDATE()与实体框架迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将EntityFramework.Migrations(Beta 1)添加到现有的Code-First应用程序,该应用程序正在进行一些更改(对于从我的代码优先API生成的表的迁移功能和更多微调),并运行进入GETDATE()方案。



我已经在我的DbContext中使用自定义的初始化类来运行SQL脚本来设置一些字段并在我的数据库中创建索引。我的一些AlterTable脚本只是设置默认值的字段(例如某些DateTime字段设置为GETDATE())。我真的希望EntityFramework.Migrations会有一个答案,因为你可以轻松地指定defaultValue,但到目前为止,我没有看到一个。



任何想法?我真的希望做以下的事情会神奇地工作。 (毕竟是'魔法独角兽')

  DateCreated = c.DateTime(nullable:false,defaultValue:DateTime.Now )

不幸的是,在逻辑上,它将我的默认值设置为更新数据库命令执行

解决方案

您必须在<$ c中使用自定义SQL脚本$ c> Up 设置默认值的方法:

  Sql(ALTER TABLE TableName ADD CONSTRAINT ConstraintName DEFAULT GETDATE()FOR ColumnName); 

在代码中设置默认值只允许静态值 - 无数据库级功能。



无论如何,如果要首先使用代码,在POCO构造函数中设置它是正确的。另外,如果要为某些特殊情况设置应用程序中的值,则无法在数据库中使用默认值,因为数据库中的默认值需要 DatabaseGeneratedOption.Identity DatabaseGeneratedOption.Computed 。这两个选项允许仅在数据库中设置属性。



编辑:



由于产品仍在开发中,我的答案已经不再有效了。通过使用 defaultValueSql (在EF Migrations Beta 1中不可用,但是已添加到EF 4.3 Beta 1中已经包含迁移,请查看@gius答案以实现此要求)。


I added EntityFramework.Migrations (Beta 1) to an existing Code-First app that is going through some changes (for both migration capabilities and more fine-tuning of the tables I am generating from my code-first API) and ran into the GETDATE() scenario.

I was already using a custom initializer class in my DbContext to run SQL scripts to set some fields and create indexes in my database. A handful of my AlterTable scripts are primary just to setup fields with default values(such as certain DateTime fields being set to GETDATE()). I was really hoping EntityFramework.Migrations would have an answer for this since you can easily specify defaultValue, but so far I'm not seeing one.

Any ideas? I was really hoping that doing the following would magically work. (It is 'magic unicorn', after all)

DateCreated = c.DateTime(nullable: false, defaultValue: DateTime.Now)

Unfortunately, and logically, it set my default value to the time when the Update-Database command was executed.

解决方案

You must use custom SQL script in Up method for setting default value:

Sql("ALTER TABLE TableName ADD CONSTRAINT ConstraintName DEFAULT GETDATE() FOR ColumnName");

Setting default value in code allows only static values - no database level functions.

Anyway setting it in POCO constructor is correct way if you are going to use code first. Also if you want to set the value in the application for some special cases you cannot use a default value in the database because the default value in the database requires either DatabaseGeneratedOption.Identity or DatabaseGeneratedOption.Computed. Both these options allow setting the property only in the database.

Edit:

Since the product is still in development my answer is no longer valid. Check @gius answer for actual way to achieve this requirement by using defaultValueSql (it wasn't available in EF Migrations Beta 1 but was added in EF 4.3 Beta 1 which already includes migrations).

这篇关于可能默认DateTime字段到GETDATE()与实体框架迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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