开发者

How to make EFCodeFirst generate a column of type int for a property of type enum?

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; }
开发者_Python百科
        [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?


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 the Sex property.

EF Code First will ignore any property that is not a primitive type when creating a DB from your model.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜