Convert program data to EF5 Code First migration

Is it possible to do any transformation of program data in Entity Framework 5 First code changes?

There is an Sql () method for executing queries, but it has a return type of void, and I see no way to get the results of the queries that I execute.

Example

I have a recipe table with a one-to-many to ingredient ratio. For various reasons, I want to convert this to a property of a string JSON component instead. The only approach I can think of is something like this:

  • Create a New IngredientsJson Column
  • For each recipe, query its ingredients, create a JSON string programmatically, and paste into a new column.
  • Drop the old table component.
+3
source share
1 answer

You should use db 'initializer' for whatever you want - and / ore 'Seed' sortings (as for where to enter the EF stream).

You can watch this post with the configured < initializer - , which runs both Db Create ... and Migrate . It doesn’t cut and paste the solution, but it basically works (it was just a quick fix to the problem, you need to correct it a bit, it has a couple of corrections below).

MigrateDatabaseToLatestVersion only part of the migration — and you need seed detection — either manually wrap this part (the main thing is in the “checks” performed for different situations, that is, when you “participate” in the migration — or sowing).

Migration should go first , and the db type 'creation' does not make much sense except sowing.

You redefine seed (you created) to put any db processing in there - you have a DbContext , and you can also call SqlQuery if necessary.


How to create an initializer to create and migrate a mysql database?

+1
source

All Articles