Drop Column from SQLite Database
Last updated
Last updated
Improve EF Core performance with EF Extensions
SQLite is relatively limited in its schema manipulation capabilities as compared to other databases. For example, dropping a column from an existing table requires that the entire table be dropped and re-created.
In EF Core 5.0, migrations now support the automatic rebuilding of the table for schema changes that require it.
Let's suppose we have a simple model that contains a Book
entity as shown below.
To create the database, let's run the following migration command in Package Manager Console.
You will see that migration creates the following script.
To apply the above script to the database, run the following command to update the database.
You will see that the database is created that contains a Books
table.
Now we want to remove the Category
property so let's remove that property from the Book
class as shown below.
Add a new migration using the following migration command.
It will create the following script to update the database.
To update the database with the above script, run the following.
Before EF Core 5.0, this update will fail, because the column cannot be dropped.
In EF Core 5.0, you will see that migrations will instead rebuild the table successfully.
A temporary table is created with the desired schema for the new table.
Data is copied from the current table into the temporary table.
Foreign key enforcement is switched off.
The current table is dropped
The temporary table is renamed to be the new table.