代码首次迁移

时间:2012-12-12 05:18:26

标签: c# asp.net-mvc entity-framework

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

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

1 个答案:

答案 0 :(得分:3)

尝试修改添加“IsSchool”的行

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