Let's say we have the following simple model:
public class Car
{
public int Year { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public CarType Type { get; set; }
}
public enum CarType
{
Car, Truck
}
Entity Framework, when adding a new Car
object to the database, will store the CarType
enum value as an integer.
If we change the CarType
enum in a way where the integer values change (change the order or add/remove values), does Entity Framework know how to properly handle the data migration using Migrations?
For example, let's say we added another value to CarType
:
public enum CarType
{
Car, Truck, Van
}
This would have no real effect on the existing data in the database. 0
is still Car
, and 1
is still Truck
. But if we changed the order of CarType
, like this:
public enum CarType
{
Car, Van, Truck
}
Database records with 1
as the CarType
to designate Truck
would be incorrect, since according to the updated model 1
is now Van
.
EF Core compares the current model against a snapshot of the old model to determine the differences, and generates migration source files; the files can be tracked in your project's source control like any other source file. Once a new migration has been generated, it can be applied to a database in various ways.
In Entity Framework, this feature will allow you to define a property on a domain class that is an enum type and map it to a database column of an integer type. Entity Framework will then convert the database value to and from the relevant enum as it queries and saves data.
To manage migrations, you must first install the EF Core command-line tools. If the DbContext is in a different assembly than the startup project, you can explicitly specify the target and startup projects in either the Package Manager Console tools or the . NET Core CLI tools.
Add-Migration: Creates a new migration class as per specified name with the Up() and Down() methods. Update-Database: Executes the last migration file created by the Add-Migration command and applies changes to the database schema.
No, Migrations does not fully support enum changes since it does not update the database values to reflect changes such as altered order, additions or removals.
Adding enum values while preserving the order will have no effect. In fact, it will not even fire a model backing change error.
If the order of the CarType
enum changes, then the database data will effectively be invalid. The original int
values are preserved, but the enum results will be wrong.
In order to accommodate this type of change, manual processing of the database data will be necessary. In this specific example, custom SQL will have to be run that changes the values of the Type
column according to the enum changes:
public partial class CarTypeChange : DbMigration
{
public override void Up()
{
// 1 now refers to "VAN", and 2 now refers to "Truck"
Sql("Update cars Set [Type] = 2 Where [Type] = 1");
}
public override void Down()
{
Sql("Update cars Set [Type] = 1 Where [Type] = 2");
}
}
Addendum: I've asked another question related to this: Handling enum changes in Entity Framework 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With