27

I am using Entity Framework 5, DB first. I know how to define an enum on my model, and set the type of a field to that enum.

Now, I have a requirement to map a field MyField to an enum that is defined externally, i.e. not in the EF model (OtherNamespace.MyEnum). The designer does not allow me to set the type to anything outside the model. I tried editing the edmx file manually, but that causes an error:

Error 10016: Error resolving item 'MyField'. The exception message is: 'Unresolved reference 'OtherNamespace.MyEnum'.'.

OtherNamespace.MyEnum is referenced by my project.

How do you do it?

asked Oct 1, 2013 at 12:27
6
  • Your going to have to do some extra logic to get that, you could put that in the setter. Commented Oct 1, 2013 at 12:35
  • @Dunbar what do you mean? Can you be more specific? Commented Oct 1, 2013 at 12:41
  • Like in the property setter, have it set a non mapped instance of the object you really want set. Commented Oct 1, 2013 at 12:42
  • @Dunbar that will work in local code, but it won't work in an IQueryable<MyClass> e.g. db.MyClasses.Where(x => x.MyField == OtherNamespace.MyEnum.Value2) Commented Oct 1, 2013 at 12:53
  • Does the enum you are implementing have known integer representations for each enum value? Commented Oct 7, 2013 at 14:33

1 Answer 1

54
+100

This can be done, but it requires a little sacrifice on the database side. Entity Framework (5 onwards) supports mapping a field to an enumeration, but only for byte, sbyte, short, ushort, int, uint, long, or ulong types.

Assume that we have the following sample table:

CREATE TABLE [People](
 [id] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
 [Name] [varchar](50) NOT NULL,
 [Title] [int] NOT NULL
)

Title has been declared as an integer. In a real database, this might be a foreign key over to a TitleTypes table.

Also, let's assume that the external enumeration that we are going to be tying into is defined as:

namespace Enumerations
{
 public enum TitleEnum
 {
 Mr,
 Mrs,
 Dr,
 None
 }
}

If we import the People table into an EDMX we can right click on the Title column and Convert to Enum

Convert To Enum

This will bring up a dialog box allowing us to specify a name for the enumeration in the EDMX ModelStore, define any values for the enumeration OR link to an external enumeration via Reference external type.

Give it a Type Name of TitleEnum, check Reference external type, and type Enumerations.TitleEnum in the provided field. Click OK and it will associate the column to the external enumeration.

Note:

  • While both are called TitleEnum, this is acting as a passthrough to the external Enumeration
  • The type of your column and the external enumeration MUST match

Linking the Enumeration

Now, when we create a new person we can utilize the enumeration and it will be translated into its Int representation.

Data.ScratchEntities context = new Data.ScratchEntities();
Data.Person person = new Data.Person();
person.Name = "Jane Smith";
//Note the use of the external enumeration here
person.Title = Enumerations.TitleEnum.Mrs;
context.People.Add(person);
context.SaveChanges();

Intellisense

answered Oct 7, 2013 at 17:04

14 Comments

Thanks for a beautifully documented answer. But it's not working for me. Everything is fine up to the last step - but I'm getting a compiler error: cannot convert source type Enumerations.TitleEnum to target type MyNamespace.TitleEnum. What am I doing wrong? Could it be because my base type is byte rather than int?
Yes. The type of the column and the base type for the enum will need to match so that it can do an implicit conversion from one to the other.
The column in the db is a tinyint, which maps to byte...? And if that were the case, an explicit cast should work - but it doesn't.
To my knowledge, when mapping enum to enum it is not possible to explicitly cast the type when setting the value. What DOES work in this situation however is to leave the EF type definition for that field as a byte and when you are setting the value from the enumerations cast the enum value to the appropriate type: td.Title = (byte)Enumerations.TitleEnum.Mr;
When you Converted to Enum in the EDMX, did you specify the external enumeration? Trying to replicate your errors, when I remove the reference, I get the exact error message you mention in the first comment above. You can edit the existing enum mapping via the Model Browser under Enum Types (sorry it took me an hour to get there, hadn't had coffee yet)
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.