简体   繁体   English

实体框架5中的代码拳头枚举未添加到数据库中

[英]Code fist enum in Entity Framework 5 not added to database

I'm having a problem getting enums to map to a database using entity framework. 我在使用实体框架将枚举映射到数据库时遇到问题。 I set up a super basic class to test my problem: 我设置了一个超级基础类来测试我的问题:

public class Person
{
    public int PersonId { get; set; }
    public Genders Gender { get; set; }

    public enum Genders
    {
        Female, Male
    }
}

With a context of: 具有以下背景:

public DbSet<Person> Person { get; set; }

I'm using MVC 4 so to create the mapping I create a new controller with Person as the Model and my context as the context. 我正在使用MVC 4来创建映射我创建了一个新的控制器,其中Person作为模型,我的上下文作为上下文。 The MVC scaffolding creates the controller, views, and DB but when I view the DB there's a table named Person with just one column - PersonId - but no Gender column . MVC脚手架创建控制器,视图和数据库但是当我查看数据库时,有一个名为Person的表只有一列 - PersonId - 但没有Gender列

Extra Info: Using MVC 4 targeting .Net 4.5 and using Entity Framework 5.0 (even double checked the dll version) and connecting to (LocalDb)\\V11.0. 额外信息:使用MVC 4定位.Net 4.5并使用Entity Framework 5.0(甚至双重检查dll版本)并连接到(LocalDb)\\ V11.0。

I've tried changing Genders to: 我试过将Genders改为:

public enum Genders : Byte
{
    Female = 0, 
    Male = 1
}

I even tried moving Genders to a separate class as one answer suggested. 我甚至尝试将Genders移动到一个单独的类中,如同一个答案所示

I found one MSDN article that sets the class up the same way I had and works, the only difference is they were using a console application instead of MVC. 我找到了一篇MSDN文章 ,它以与我工作相同的方式设置类,唯一的区别是它们使用的是控制台应用程序而不是MVC。

Am I missing something??? 我错过了什么吗???

You have to define the enum outside any class. 你必须在任何之外定义枚举。 Do not embed it in a class. 不要把它嵌入课堂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM