I am trying to save an Enum that exists in my model down to the database but everytime I do Entity Framwework complains that there is no look up table associated with the Enum. I do not want a look up table, I just want the enum to exist in code and be stored as integers in the database.
An error occurred while saving
entities that do not expose foreign
key properties for their
relationships. The EntityEntries
property will return null because a
single entity cannot be identified as
the source of the exception. Handling
of exceptions while saving can be made
easier by exposing foreign key
properties in your entity types. See
the InnerException for details.“Cannot insert the value NULL into
column ‘Type’, table
‘Test.dbo.Interests’; column
does not allow nulls. INSERT
fails.\r\nThe statement has been
terminated.
The value for Type is definitely not null, EF just treats it that way cause it can’t find a Foreign key table that I don’t want in the first place.
How do I get entity framework to treat my enum as an int and then convert back out when I call down to the database to retrieve my model?
Entity framework doesn’t support enums at all so you cannot map enum property to the database. You must use int property instead and if you want enum as well you must define another not mapped property (in case of autogenerated entities you must define it in your partial class) which converts int to enum.Edit:
Enum support was added in later version. If you want to map enums (integer values, not strings at the moment) you need to use .NET 4.5 and EF5 or EF6.x and .NET 4.x.