繁体   English   中英

代码首次迁移

[英]Code first migrations

我有一个类,我想添加一些列如下:

public partial class News : DbMigration
{
    public override void Up()
    {

        AddColumn("dbo.News", "IsSchool", c => c.Boolean());
        AddColumn("dbo.News", "City", c => c.String(maxLength: 200));
        AddColumn("dbo.News", "County", c => c.String(maxLength: 200));
        AddColumn("dbo.News", "SchoolName", c => c.String(maxLength: 200));
        AddColumn("dbo.News", "VisitDatetime", c => c.DateTime());

        Sql("UPDATE dbo.News SET IsSchool='false'");
    }

    public override void Down()
    { 
        DropColumn("dbo.News", "VisitDatetime");
        DropColumn("dbo.News", "County");
        DropColumn("dbo.News", "City");
        DropColumn("dbo.News", "IsSchool");
        DropColumn("dbo.News", "SchoolName"); 
    }
}

这是配置:

  public sealed class Configuration : DbMigrationsConfiguration<DataContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
    }

    protected override void Seed(DataContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //
    }
}

我只想再运行一次,看看数据库是否会更新? 我如何确保此迁移确实运行?

我无法访问数据库,我的应用程序一直在抱怨IsSchool字段可以为Nullable

尝试修改添加“IsSchool”的行

AddColumn("dbo.News", "IsSchool", c => c.Boolean(nullable: false, defaultValue: false));

暂无
暂无

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

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