简体   繁体   English

如何将查找表映射到枚举?

[英]How to map lookup table to enum?

Suppose i have the following 2 SQL tables: 假设我有以下2个SQL表:

Foo

Column        DataType
---------------------------
Title         NVARCHAR(20)
Body          NVARCHAR(MAX)
FooTypeId     TINYINT

FooType FooType

Column        DataType
--------------------------
FooTypeId     TINYINT
Name          NVARCHAR(10)

Now, im using Entity Framework 4.0 with a custom data context and POCO implementation. 现在,我正在使用Entity Framework 4.0与自定义数据上下文和POCO实现。

How do i map this on the designer, and my POCO's? 我如何在设计师和我的POCO上映射这个?

Do i have to create a POCO property (of type byte i assume) called "FooTypeId", then i expose ANOTHER property of my enum type? 我是否必须创建一个名为“FooTypeId”的POCO属性(字节我假设),然后我公开我的枚举类型的另一个属性?

Ie. IE浏览器。

public class Foo
{
    public byte FooTypeId { get; set; } // for ORM - do i need this??
    public FooType FooType // for most querying operations
    {
         get
         {
            return (FooType)this.FooTypeId;
         }
         set
         {
            this.FooTypeId = (int)value;
         }
    }
}

public enum FooType
{
    Blah = 1,
    Foo = 2,
    Bar = 3
}

At the moment, i do not even have the FooType table on my designer, as i figured i can try and "express" this as an enumeration from the actual FooTypeId on the Foo property. 目前,我甚至没有设计器上的FooType表,因为我想我可以尝试“表达”这个作为Foo属性上实际FooTypeId的枚举。 Or am i supposed to create a "Navigational Property" on the mapper, then define that in my POCO? 或者我应该在mapper上创建一个“Navigational Property”,然后在我的POCO中定义它?

I've read threads from a few years back (EF1) saying "Enums are not supported in EF", is this still the case with EF4? 我读过几年前的线程(EF1)说“EF不支持枚举”,EF4的情况是否仍然如此? If it is, is what im doing right? 如果是的话,我正在做什么?

I'm kind of lost here, some guidance would be greatly appreciated! 我有点迷失在这里,一些指导将不胜感激!

Currently, EF4 doesn't support enums natively. 目前,EF4本身不支持枚举。

I've seen a great workaround by AlexJ that works pretty well (it's pretty code heavy though), http://blogs.msdn.com/b/alexj/archive/2009/06/05/tip-23-how-to-fake-enums-in-ef-4.aspx 我已经看到了AlexJ的一个很好的解决方法,它运行得很好(虽然代码很重), http://blogs.msdn.com/b/alexj/archive/2009/06/05/tip-23-how-to -fake-枚举功能于EF-4.aspx

I've also heard that this native enum support is coming in the next version of EF4, but who knows exactly when that will be released. 我也听说过这个本机枚举支持将在EF4的下一个版本中出现,但是谁知道它将在何时发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM