如何将查找表映射到枚举?
假设我有以下 2 个 SQL 表:
Foo
Column DataType
---------------------------
Title NVARCHAR(20)
Body NVARCHAR(MAX)
FooTypeId TINYINT
FooType
Column DataType
--------------------------
FooTypeId TINYINT
Name NVARCHAR(10)
现在,我使用带有自定义数据上下文和 POCO 实现的 Entity Framework 4.0。
我如何将其映射到设计器和我的 POCO 上?
我是否必须创建一个名为“FooTypeId”的 POCO 属性(我假设为字节类型),然后公开我的枚举类型的另一个属性?
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
}
目前,我的设计器上什至没有 FooType 表,因为我想我可以尝试将其“表达”为 Foo 属性上实际 FooTypeId 的枚举。 或者我应该在映射器上创建一个“导航属性”,然后在我的 POCO 中定义它?
我读过几年前 (EF1) 的帖子,说“EF 不支持枚举”,EF4 仍然是这种情况吗?如果是的话,我做的对吗?
我有点迷失在这里,非常感谢一些指导!
Suppose i have the following 2 SQL tables:
Foo
Column DataType
---------------------------
Title NVARCHAR(20)
Body NVARCHAR(MAX)
FooTypeId TINYINT
FooType
Column DataType
--------------------------
FooTypeId TINYINT
Name NVARCHAR(10)
Now, im using Entity Framework 4.0 with a custom data context and POCO implementation.
How do i map this on the designer, and my POCO's?
Do i have to create a POCO property (of type byte i assume) called "FooTypeId", then i expose ANOTHER property of my enum type?
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.
Or am i supposed to create a "Navigational Property" on the mapper, then define that in my 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? If it is, is what im doing right?
I'm kind of lost here, some guidance would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前,EF4 本身不支持枚举。
我看到 AlexJ 提供了一个很好的解决方法,效果很好(虽然代码量很大),http://blogs.msdn.com/b/alexj/archive/2009/06/05/tip-23- how-to-fake-enums-in-ef-4.aspx
我还听说 EF4 的下一版本将提供这种本机枚举支持,但谁知道具体何时发布。
Currently, EF4 doesn't support enums natively.
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
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.