Change data in migration Up method - Entity Framew

2020-02-08 06:58发布

I have added a new property into my existing model. It's a bool property with default value true. There are existing data in this table and I would like to set one specific row's new property to false right after creating the new field, in the Up method.

public override void Up()
    {
        AddColumn("dbo.RequestValidationErrors", "IsBreaking", c => c.Boolean(nullable: false));
        using (Context ctx = new Context())
        {
            var validation = ctx.RequestValidationErrorSet.FirstOrDefault(x => x.WordCode == "RequestValidationError.MoreThanOneItemFound");
            if (validation != null)
            {
                validation.IsBreaking = false;
                ctx.SaveChanges();
            }
        }
    }

This way EF throws an error during saying

System.InvalidOperationException: The model backing the 'DbContext' context has changed since the database was created. Consider using Code First Migrations to update the database

Is it possible to change the database here or should I do it elsewhere?

4条回答
姐就是有狂的资本
2楼-- · 2020-02-08 07:15

If you want to use the framework for changes like this, you should separate Database changes from Data changes.

Create a migration for just the Database changes, and execute.

Then create a new migration (the Up() and Down() methods will be empty). You can now instantiate your DatabaseContext and there will be no error. This way you can use the Framework for these changes, and properly implement a Down() method.

查看更多
\"骚年 ilove
3楼-- · 2020-02-08 07:17

In the middle of a migration, it's better to use Sql() method to update database data.

Sql("UPDATE dbo.RequestValidationErrors SET IsBreaking = 0 WHERE WordCode = 'RequestValidationError.MoreThanOneItemFound'");

Also you should define the default value for the new column. So the solution should be something like this:

public override void Up()
{
    AddColumn("dbo.RequestValidationErrors", "IsBreaking", c => c.Boolean(nullable: false, default: true));
    Sql("UPDATE dbo.RequestValidationErrors SET IsBreaking = 0 WHERE WordCode = \"RequestValidationError.MoreThanOneItemFound\"");
}

Using a DbContext in the middle of it's migration is very ambiguous. What do you expect from the context? It has the after migration state in its models, but the database has the before migration state in the tables. So the model and database are not match. If you still insist on using DbContext in your code, disabling model checking might be the solution. You can disable model checking using:

Database.SetInitializer<Log4ProContext>(null);
查看更多
虎瘦雄心在
4楼-- · 2020-02-08 07:30

Writing DataMigrations for EF6 can be a chore. We put together a library I'm just open sourcing here for others to use, that adds in this long-promised, missing feature to EF6. Just write classes using regular EF queries etc.

https://github.com/b9chris/Brass9.Data

查看更多
够拽才男人
5楼-- · 2020-02-08 07:35

Instead of using the Sql method you could also use the UpdateData method.

migrationBuilder.UpdateData(
    table: "RequestValidationErrors", 
    keyColumn: "WordCode", 
    keyValue: "RequestValidationError.MoreThanOneItemFound", 
    column: "IsBreaking", 
    value: false);

(I don't know if only ef core supports this method)

查看更多
登录 后发表回答