Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

c# - How to use an existing enum with Entity Framework DB First

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...