Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF-具有自动迁移功能的新列的默认值_C#_Entity Framework_Ef Code First_Entity Framework Migrations_Automatic Migration - Fatal编程技术网

C# EF-具有自动迁移功能的新列的默认值

C# EF-具有自动迁移功能的新列的默认值,c#,entity-framework,ef-code-first,entity-framework-migrations,automatic-migration,C#,Entity Framework,Ef Code First,Entity Framework Migrations,Automatic Migration,我首先使用EF代码和自动迁移。我想在我的模型中添加一个新列—一个布尔列,表示“活动”(true)或“非活动”(false)。如何添加此列并为数据库中已存在的行设置默认值(“true”)——使用自动迁移?Tamar,您需要设置默认值,请参阅下一个示例: namespace MigrationsDemo.Migrations { using System; using System.Data.Entity.Migrations; public partial cla

我首先使用EF代码和自动迁移。我想在我的模型中添加一个新列—一个布尔列,表示“活动”(true)或“非活动”(false)。如何添加此列并为数据库中已存在的行设置默认值(“true”)——使用自动迁移?

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

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"); 
        } 
    } 
} 

EF 7将支持模型中的默认值