这是一个模型:
Public class Person { [Key] Public int PersonId { get; set: } Public int Age { get; set; } Public ColorEnum FavoriteColor { get; set; } } Public Enum ColorEnum { Red = 1,Green = 2,Blue = 3 }
Entity Framework Code First是否可以使用Person模型生成相应的表? ColorEnum类型怎么样?
谢谢
解决方法
EF 4.3不支持Enums.但是已经宣布了
Enum
support is coming with EF 5,它将与.NET 4.5一起推出.要使用Code-First处理枚举,您将执行以下操作:
Public class Person { [Key] Public int PersonId { get; set: } Public int Age { get; set; } public int FavoriteColorValue{ get; set;} [NotMapped] Public ColorEnum FavoriteColor { get{ return (ColorEnum)FavoriteColorValue; } set{ FavoriteColorValue = (int)value; } } } Public Enum ColorEnum { Red = 1,Blue = 3 }