Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Enums with Code First & Entity Framework 5

Just trying to confirm an impression: it seems enums in EF5 + Code First are only supported when declared within the same namespace as the classes using them as property types.

Can anyone confirm that? Couldn't find anything on the web for this...

like image 341
guidupuy Avatar asked Mar 19 '12 11:03

guidupuy


People also ask

How do you use enums correctly?

You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values. Examples would be things like type constants (contract status: "permanent", "temp", "apprentice"), or flags ("execute now", "defer execution").

Do C# enums start at 0 or 1?

The first member of an enum will be 0, and the value of each successive enum member is increased by 1. You can assign different values to enum member.

What number do enums start with?

Enum Values By default, the first item of an enum has the value 0.

Should you store enums in database?

By keeping the enum in your database, and adding a foreign key on the table that contains an enum value you ensure that no code ever enters incorrect values for that column. This helps your data integrity and is the most obvious reason IMO you should have tables for enums.


1 Answers

A relevant bug that was fixed earlier. 4.3 Beta 1 release notes say:

Bug fix for GetDatabaseValues. In earlier releases this method would fail if your entity classes and context were in different namespaces. This issue is now fixed and the classes don’t need to be in the same namespace to use GetDatabaseValues.

My guess is that GetDatabaseValues function is still buggy for this occasion (but that's just an educated guess). You may want to report this here: ADO.NET team blog: EF5 Beta 1 Available

like image 147
Teoman Soygul Avatar answered Sep 28 '22 18:09

Teoman Soygul