How to access context after each migration of Entity Framework db

When I add-Migration, I get the corresponding DbMigration class with Up / Down methods, where I can make schema changes and (using the Sql () method) can also modify data / content.

I would like to be able to make content changes for each migration using a database context. I understand that I can use the Seed method in the Configuration class, but I understand that I can only connect one configuration using my initializer.

I would prefer to use the UpCompleted () / DownCompleted () methods, which would provide access to the db context after the migration is complete. This will allow incremental data / context change scripts to be written in a way that is less error prone than using the Sql () method.

Am I missing something? Is it possible?

Thanks!

+6
source share
1 answer

This does not work, because the context has only the latest model, which can only be used to access the database after the last migration has been started (which is effectively achieved by seed).

An example of how this idea breaks if you move a property from one class to another, then the seed logic from older migrations will no longer compile. But you cannot change it to use the new property because the corresponding column did not exist in the database yet.

If you want to write such seed / data processing logic, you need to put it at the end of the Up / Down methods and use the Sql method to execute it using raw SQL.

~ Rowan

+8
source

Source: https://habr.com/ru/post/970264/


All Articles