3

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

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.