Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use MigrationBuilder DeleteData to delete all rows during a migration?

The documentation is not very helpful - just a list of function signatures, of which the simplest is:

public virtual OperationBuilder DeleteData (string table, string keyColumn, object keyValue, string schema = null);

I am fairly sure I can work out what goes in table, keyColumn and schema, but what value should I put in keyValue if I want to delete all rows?

like image 652
Rupert Morrish Avatar asked Mar 23 '18 02:03

Rupert Morrish


People also ask

Does migration delete data?

Migrations are Django's way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. So, deleting migration files does not affect the data in the database.

How do I delete a specific migration in .NET core?

Delete the row corresponding to your migration that you want to unapply (Say "yes" to the warning, if prompted). Run "dotnet ef migrations remove" again in the command window in the directory that has the project. json file. Alternatively, run "Remove-Migration" command in the package manager console.


1 Answers

doesn't look like the DeleteData operations are designed to allow for a delete everything in the table operation. EF uses the keyColumn and keyValue to determine what to delete, i.e. where keyColumn = keyValue. I would recommend using something like this:

migrationBuilder.Sql("DELETE FROM [table]", true);

https://github.com/dotnet/efcore/blob/main/src/EFCore.Relational/Migrations/Operations/DeleteDataOperation.cs

https://github.com/dotnet/efcore/blob/main/src/EFCore.Relational/Migrations/MigrationBuilder.cs (about line 1400)

like image 81
jcwmoore Avatar answered Sep 19 '22 05:09

jcwmoore