Albert Bori
Albert Bori

Reputation: 10002

EF Code First - Run post-migration processing and cleanup

I am using Entity Framework 5.0 and have implemented custom migrations.

One thing that I'd like to do is perform some calculation and clean-up jobs after a specific migration is completed. I know that the Seed() method is used for post-migration data manipulation, but it is run with every migration. I want this to only run once after a specific migration is complete.

I would do it inside of the custom migration itself, but the processes require that I pull result sets and perform calculations on them, etc. and I'd like to do this in C# if possible (to utilize our job processing system).

What is the best way to run post-migration processing code for a specific migration?

Upvotes: 2

Views: 776

Answers (1)

Colin
Colin

Reputation: 22595

I guess you could target a specific migration by interrogating the __MigrationHistory table. But that strikes me as being a bit fragile because you might want to re-scaffold migrations at some point. I think I would prefer to trigger the cleanup based on some other criteria.

Is there any way to detect that the data is "dirty"? Then the cleanup could be triggered when necessary.If that's not possible you could create a table in the database to hold flags that indicate when a cleanup is necessary. You could set the flag in the Up() method and remove it in the Down() method in a migration. Then you can check for the flag in the Seed() method and trigger the cleanup.

I think these are good Tips for Entity Framework Migrations

Upvotes: 1

Related Questions