Create new table, copy data to new table, delete in code used properties while migrating. Possible within ONE migration?

Viewed 13


I need your advise.

Main goal is to archive this with ONE migration (file):
"Move" n columns from a data containing table "X" to a new table "Y",
aggregate the data of table "X" regarding the n columns,
store them in the new table "Y",
add a FK in table "X" to point to the new table "Y" and
delete the no longer needed properties in class "X" that referencing the "copied" columns in table "X"!!!

I have already a working solution but it needs two migration steps.

Can this be done during one migration step?
Including also the deletion of those properties in table "X" which
are no longer needed after the migration?

Do I miss some MS documentation?



My working solution:
1) Code changes / steps part 1
1a) Add new class "Y" (to reference table "Y" later on)
1b) Adjust classes "X" and "Y" to introduce a FK pointing from table "X" to table "Y".
1c) Create migration file (via "add-migration added_table_Y").
1b) "Inject" the migration process in such a way that our own implemented code is worked out.
1c) This "own implemented code"
1c1) reads all values of table "X",
1c2) aggregates the read rows,
1c3) adds the resulting rows into table "Y",
1c4) updates the new reference property ("public int Yid ...") in class/table "X"
and
1c4) adds a foreign key pointing from table "X" to table "Y".

2) First migration
2a) Start the app.
Result:
The migration file and our own implemented code will be worked out.
New table is introduced, data are "copied", FK is well set up.

ToDo:
Remove the now longer needed properties in class "X" referencing columns in table "X".

3) Code changes / steps part 2
3a) Delete all properties referencing those columns of table "X" that will be deleted.
3b) Delete the code created in step 1c) because for sure it references for reading purpose
properties you deleted during step 3a).
3c) Fire "add-migration ..." in "Package Manager Console"

4) Second migration
4a) Start the app.
Result:
The newly created migration file is worked out.
The columns in table "X" are removed.
The properties of those columns are removed from the code.


Done.
0 Answers
Related