1

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

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

4

1 回答 1

3

尝试修改添加“IsSchool”的行

AddColumn("dbo.News", "IsSchool", c => c.Boolean(nullable: false, defaultValue: false));
于 2012-12-12T09:28:16.090 回答