如何使用实体框架数据库首先将现有枚举

如何使用实体框架数据库首先将现有枚举

本文介绍了如何使用实体框架数据库首先将现有枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架5,DB第一。我知道如何在我的模型定义枚举,并设置一个字段是枚举类型。

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.

现在,我有一个要求,映射字段 MyField的来是外部定义枚举,即没有在EF模型( OtherNamespace.MyEnum )。设计者不允许我的类型设置为模型以外的任何东西。我试图手动编辑EDMX文件,但会导致一个错误:

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:

错误10016:错误解决项目'MyField的。异常消息为:。。未解决的参考'OtherNamespace.MyEnum

OtherNamespace.MyEnum 是由我的项目中引用。

OtherNamespace.MyEnum is referenced by my project.

你怎么办呢?

推荐答案

这是可以做到的,但它需要在数据库端一个小的牺牲。实体框架(5起)支持的字段映射到一个枚举,但仅限于字节为sbyte USHORT INT UINT ULONG 类型。

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
)

标题已被宣布为整数。在实际的数据库中,这可能是转移到 TitleTypes 表的外键。

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
    }
}

如果我们导入了<$​​ C $ C>人表到EDMX我们可以右键点击标题列和转换为枚举

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

这将弹出一个对话框,允许我们为枚举中指定一个名称在EDMX ModelStore,定义枚举的任何值的的通过链接到外部枚举引用外部类型

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.

给它 TitleEnum 的类型名称,检查引用外部类型,然后键入 Enumerations.TitleEnum 在提供的字段。点击OK,它会列到外部枚举关联。

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.

注:


  • 当这两个被称为TitleEnum,这是作为一个直通到外部枚举

  • 类型的列和外部枚举必须匹配

  • 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

现在,当我们创建一个新的人,我们可以利用枚举,它会转化为它的智力表现。

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();



这篇关于如何使用实体框架数据库首先将现有枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 22:05