假设我们有以下简单模型:
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
}
实体框架在向数据库添加新Car
对象时,会将CarType
枚举值存储为整数。
如果我们CarType
以整数值更改的方式更改枚举(更改顺序或添加/删除值),Entity Framework 是否知道如何使用 Migrations 正确处理数据迁移?
例如,假设我们添加了另一个值CarType
:
public enum CarType
{
Car, Truck, Van
}
这对数据库中的现有数据没有实际影响。0
还在Car
,1
现在还在Truck
。但是如果我们改变 的顺序CarType
,像这样:
public enum CarType
{
Car, Van, Truck
}
1
使用asCarType
指定的数据库记录Truck
是不正确的,因为根据更新的模型1
是 now Van
。