ev vk
ev vk

Reputation: 355

Use enum in Entity Framework Table :what for?

Good day,

I have been learning some Entity Framework and I came to this code

public   class Test
{
    public int Id { get; set; }
    public string name { get; set; }
    public testEnum testEnum { get; set; }

}
public enum testEnum {
    numero1=1,
    numero2=2
} 

Then I run add-migration xxxxx and update-database, the problem is that when I go to SQL server, I can not see the enum anywhere. My questions are: 1.What happened with the enum and how can I see it in SQL server? 2. When do I use enum instead of a table like the following:

public class EnumReplace{ public int Id { get; set; }
 public int value{ get; set; }
}

thank you.

Upvotes: 0

Views: 3036

Answers (2)

ashin
ashin

Reputation: 2603

1.What happened with the enum and how can I see it in SQL server?

The enum gets stored in the database table as an integer. You wouldn't be able to see the enumeration in the database. EF takes the responsibility of casting the underlying type to the enumeration defined while retrieving the values from the database.

In Entity Framework, an enumeration can have the following underlying types: Byte, Int16, Int32, Int64 , or SByte. Read more about enum support in EF code first here

2. When do I use enum instead of a table like the following:

When we have a predefined set of values allowed for a property/field, we usually use enumeration. For e.g.

enum Days 
{ 
 Sat, 
 Sun, 
 Mon, 
 Tue, 
 Wed, 
 Thu, 
 Fri
};

You could also use Flagged Enums, Read more about enum here.

Upvotes: 2

Daniel
Daniel

Reputation: 3374

you have to have the enum in your class. something like this:

 public testEnum MyProperty{ get; set; }

so basically you can't have enum as a table. you can have enum as a type of one of your properties in a table.

Upvotes: 0

Related Questions