Pawan Nogariya
Pawan Nogariya

Reputation: 8980

How to rollback data to previous data in EF migration down script?

In my database table I have audit columns for create, update users and dates.

I want to update some rows using this migration script

update table set column_1=1, UpdatedBy='SystemUser', UpdatedDate=GETDATE() where column_1=2;

Now in the down script I want to revert it to the exact same state as it was before this migration. Ideally that is how it should work, right?

So I would have a down script that will do the opposite of the above Up script. The problem is how should I know what was the updated date before the update of the rows?

So this should be my down script

update table set column_1=2, UpdatedBy='SystemUser', UpdatedDate=[WHAT_SHOULD_COME_HERE] where column_1=1;

How these type of previous data dependent down scripts are written?

Upvotes: 0

Views: 55

Answers (1)

Brando Zhang
Brando Zhang

Reputation: 28247

If you don't have backup the date before, its impossible to tell the EF down script to update the before date.

But if you have customize the EF up method to update the date, something like store the date inside another table like audit table. Then you could also write the down script to set the date according to the audit table.

All in all, the most easily way for this is backup the database when you do all the migration for your database.

Upvotes: 0

Related Questions