繁体   English   中英

EF - 具有自动迁移的新列的默认值

[英]EF - Default value for new column with automatic migration

我首先使用EF代码并自动迁移。 我想在我的模型中添加一个新列 - 一个布尔列,用于显示“active”(true)或“inactive”(false)。 如何添加此列并为数据库中已有的行设置默认值(“true”) - 具有自动迁移功能?

添马舰,您需要设置默认值,请参阅下一个示例:

namespace MigrationsDemo.Migrations 
{ 
    using System; 
    using System.Data.Entity.Migrations; 

    public partial class AddPostClass : DbMigration 
    { 
        public override void Up() 
        { 
            CreateTable( 
                "dbo.Posts", 
                c => new 
                    { 
                        PostId = c.Int(nullable: false, identity: true), 
                        Title = c.String(maxLength: 200), 
                        Content = c.String(), 
                        BlogId = c.Int(nullable: false), 
                    }) 
                .PrimaryKey(t => t.PostId) 
                .ForeignKey("dbo.Blogs", t => t.BlogId, cascadeDelete: true) 
                .Index(t => t.BlogId) 
                .Index(p => p.Title, unique: true); 

            AddColumn("dbo.Blogs", "Rating", c => c.Int(nullable: false, defaultValue: 3)); 
        } 

        public override void Down() 
        { 
            DropIndex("dbo.Posts", new[] { "Title" }); 
            DropIndex("dbo.Posts", new[] { "BlogId" }); 
            DropForeignKey("dbo.Posts", "BlogId", "dbo.Blogs"); 
            DropColumn("dbo.Blogs", "Rating"); 
            DropTable("dbo.Posts"); 
        } 
    } 
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM