Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change data in migration Up method - Entity Framework

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?

like image 824
Perrier Avatar asked Nov 04 '15 08:11

Perrier


People also ask

How do I update my database in migration?

After creating a migration file using the add-migration command, you have to update the database. Execute the Update-Database command to create or modify a database schema. Use the –verbose option to view the SQL statements being applied to the target database.

What is up and down in migration?

The up method is called when migrating “up” the database – forward in time – while the down method is called when migrating “down” the database – or, back in time. In other words, the up method is a set of directions for running a migration, while the down method is a set of instructions for reverting a migration.


1 Answers

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 its 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 do not match. If you still insist on using DbContext in your code, disabling the model checking might be the solution. You can disable model checking using:

Database.SetInitializer<Log4ProContext>(null); 
like image 61
mehrandvd Avatar answered Oct 21 '22 14:10

mehrandvd