How is interpreted an enum type with EF Code First

匿名 (未验证) 提交于 2019-12-03 03:06:01

问题:

Here is a model:

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 } 

Is it possible for Entity Framework Code First to use the Person model to generate the corresponding table? What about the ColorEnum type?

Thanks

回答1:

EF 4.3 doesn't support Enums. But is has been announced that Enum support is coming with EF 5, which is due out alongside .NET 4.5. To handle enums with Code-First currently you'll do something like the following:

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,     Green = 2,     Blue = 3 } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!