Working with the Entity Frameworks migrations successfully in our project.
But now I run into a special case where I need to update a table in need of some business logic (located in our application as C# code). So I tried to spawn a thread in the migrations Up
method and doing this table update with the business logic.
This table update is not required for the application execution and should be occur in the background.
I am doing this somewhat like this:
public partial class MyMigration : DbMigration
{
public override void Up()
{
// ... do some sql migration here ...
// after executing the sql migrations custommethod should run
// migration seems to wait until CustomMethod finished work
new Thread(() => ExecuteCustomMethodDatabaseContext()).Start();
}
}
I would expect that the Up
method returns after starting the thread and EF sets the migration in the MigrationHistory to done. Thus the app can start and somewhere in the background the table gets updated.
But not so, the migrations seems to run while the thread runs (this takes a LOT of time).
So my according questions:
Open the Package Manager Console from Tools → Library Package Manager → Package Manager Console and then run the enable-migrations command (make sure that the default project is the project where your context class is).
The Up and Down methods on DbMigration merely build up an in-memory model that later gets translated to SQL. You have two options for executing custom logic:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With