在实体框架迁移必填项默认值? [英] Default value for Required fields in Entity Framework migrations?
问题描述
我已经添加了 [必需]
数据标注为我的模特之一<一个href=\"http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-validation-to-the-model\">ASP.NET MVC应用程序。创建迁移,在下面的错误运行更新数据库
resuilts后:
无法插入NULL值插入列'主任',表
MOVIES_cf7bad808fa94f89afa2e5dae1161e78.dbo.Movies';列不
允许空值。更新失败。该语句已终止。
块引用>这是因为在他们的
总监
列NULL一些记录。我如何可以自动改变这些值来一些默认的(比如张三)导演?下面是我的模型:
公共类电影
{
公众诠释ID {搞定;组; }
[需要]
公共字符串名称{搞定;组; } [数据类型(DataType.Date)
公众的DateTime RELEASEDATE {搞定;组; } [需要]
公共字符串类型{搞定;组; } [范围(1,100)
[数据类型(DataType.Currency)
公共十进制价格{搞定;组; } [StringLength(5)]
公共字符串等级{搞定;组; } [必填] ///&LT; ---新
公共字符串董事{搞定;组; }
}和这里是我最新的迁移:
公共部分类AddDataAnnotationsMig:DbMigration
{
公共覆盖无效向上()
{
AlterColumn(dbo.Movies,标题,C = GT; c.String(可空:FALSE));
AlterColumn(dbo.Movies,流派,C = GT; c.String(可空:FALSE));
AlterColumn(dbo.Movies,等级,C = GT; c.String(最大长度:5));
AlterColumn(dbo.Movies,导演,C = GT; c.String(可空:FALSE));
} 公共覆盖无效向下()
{
AlterColumn(dbo.Movies,导演,C = GT; c.String());
AlterColumn(dbo.Movies,等级,C = GT; c.String());
AlterColumn(dbo.Movies,流派中,c =&GT; c.String());
AlterColumn(dbo.Movies,标题,C = GT; c.String());
}
}
解决方案如果我没记错的话,这样的事情必须工作:
AlterColumn(dbo.Movies,导演,C = GT; c.String(可空:假的,defaultValueSql:李四));
I've added
[Required]
data annotation to one of my models in ASP.NET MVC application. After creating a migration, runningupdate-database
resuilts in the following error:Cannot insert the value NULL into column 'Director', table 'MOVIES_cf7bad808fa94f89afa2e5dae1161e78.dbo.Movies'; column does not allow nulls. UPDATE fails. The statement has been terminated.
This is due to some records having NULL in their
Director
columns. How can I automatically change those values to some default (say "John Doe") director?Here is my model:
public class Movie { public int ID { get; set; } [Required] public string Title { get; set; } [DataType(DataType.Date)] public DateTime ReleaseDate { get; set; } [Required] public string Genre { get; set; } [Range(1,100)] [DataType(DataType.Currency)] public decimal Price { get; set; } [StringLength(5)] public string Rating { get; set; } [Required] /// <--- NEW public string Director { get; set; } }
and here is my latest migration:
public partial class AddDataAnnotationsMig : DbMigration { public override void Up() { AlterColumn("dbo.Movies", "Title", c => c.String(nullable: false)); AlterColumn("dbo.Movies", "Genre", c => c.String(nullable: false)); AlterColumn("dbo.Movies", "Rating", c => c.String(maxLength: 5)); AlterColumn("dbo.Movies", "Director", c => c.String(nullable: false)); } public override void Down() { AlterColumn("dbo.Movies", "Director", c => c.String()); AlterColumn("dbo.Movies", "Rating", c => c.String()); AlterColumn("dbo.Movies", "Genre", c => c.String()); AlterColumn("dbo.Movies", "Title", c => c.String()); } }
解决方案If I am remember correctly, something like this must work:
AlterColumn("dbo.Movies", "Director", c => c.String(nullable: false, defaultValueSql: "John Doe"));
这篇关于在实体框架迁移必填项默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!