如何使 EFCodeFirst 为 enum 类型的属性生成 int 类型的列?
我的模型如下:
namespace MvcApplication1.Models
{
public enum Sex { Male, Female }
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
[Required(ErrorMessage = "Please select either Female or Male.")]
public Sex? Sex { get; set; }
}
}
我使用 EFCodeFirst 成功生成了一个数据库。不幸的是,数据库中仅创建了 Id
和 Name
列,并且没有生成 Sex
列。
如何使 EFCodeFirst 为 enum 类型的属性生成 int 类型的列?
I have a model as follows:
namespace MvcApplication1.Models
{
public enum Sex { Male, Female }
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
[Required(ErrorMessage = "Please select either Female or Male.")]
public Sex? Sex { get; set; }
}
}
I successfully generated a database using EFCodeFirst. Unfortunately, only Id
and Name
columns are created in the database and no Sex
column is generated.
How to make EFCodeFirst generate a column of type int for a property of type enum?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
截至 EF CTP5,仍不支持枚举。这是 EF 团队正在努力的事情,我们很可能会在 RTM 上得到它。现在,您必须使用
int?
作为Sex
属性。从模型创建数据库时,EF Code First 将忽略任何不是基本类型的属性。
As of EF CTP5, enums are still NOT supported. This is something that the EF team are working on though and we will likely get it on the RTM. For now you would have to use an
int?
for theSex
property.EF Code First will ignore any property that is not a primitive type when creating a DB from your model.
您可以使用一个简单的解决方法:
http://daniel.wertheim.se/2010/06/09/dealing-with-enumerations-and-entity-framework-4-code-first/
..但你可能必须这样做一旦 EF 支持枚举,就进行一些重构。
There is a simple workaround you can use:
http://daniel.wertheim.se/2010/06/09/dealing-with-enumerations-and-entity-framework-4-code-first/
..but you might have to do a little refactoring once EF supports enums.