Entity Framework 5的新Migrations功能是否完全支持枚举更改? [英] Does the new Migrations feature of Entity Framework 5 fully support enum changes?
问题描述
public class Car
{
public int年{get;组; }
public string Make {get;组; }
public string Model {get;组; }
public CarType Type {get;组;
}
public enum CarType
{
汽车,卡车
}
实体框架当向数据库添加新的 Car 对象时,将存储
CarType
枚举值作为整数。
如果我们以一种方式更改 CarType
枚举整数值更改(更改顺序或添加/删除值), Entity Framework是否知道如何使用迁移正确处理数据迁移?
例如,假设我们在 CarType
中添加了另一个值:
public enum CarType
{
Car,Truck,Van
}
这对数据库中的现有数据没有真正的影响。 0
仍然是 Car
,而 1
仍然是卡车
。但是如果我们更改了 CarType
的顺序,就像这样:
public枚举CarType
{
Car,Van,Truck
}
将 1
作为 CarType
指定卡车
的数据库记录不正确,因为根据更新的模型 1
现在 Van
。
否,迁移不不支持枚举更改,因为它不更新数据库值以反映更改,例如更改的顺序,添加或删除。
在保留顺序的同时添加枚举值将不起作用。实际上,它甚至不会启动模型支持更改错误。
如果 CarType
枚举的顺序更改,那么数据库数据将无效。原始 int
值被保留,但是枚举结果将是错误的。
为了适应这种类型更改,手动处理数据库数据将是必要的。在此特定示例中,必须运行自定义SQL,根据枚举更改更改 Type
列的值:
public partial class CarTypeChange:DbMigration
{
public override void Up()
{
// 1现在指 VAN,现在2指卡车
Sql(更新汽车集[Type] = 2其中[Type] = 1);
}
public override void Down()
{
Sql(更新车集[Type] = 1其中[Type] = 2);
}
}
附录:我已经提出了与此有关的另一个问题:处理实体框架中的枚举更改5
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
.
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
这篇关于Entity Framework 5的新Migrations功能是否完全支持枚举更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!