简体   繁体   中英

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. 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 .

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.

I've tried changing Genders to:

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

I even tried moving Genders to a separate class as one answer suggested.

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.

Am I missing something???

You have to define the enum outside any class. Do not embed it in a class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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