C# 从字符串映射枚举
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/211875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mapping Enum from String
提问by Barry
I have a string column in a database table which maps to an Enum in code. In my dbml file when I set the "Type" to MyTypes.EnumType
I get the following error:
我在数据库表中有一个字符串列,它映射到代码中的枚举。在我的 dbml 文件中,当我将“类型”设置为MyTypes.EnumType
我收到以下错误:
Error 1 DBML1005: Mapping between DbType 'VarChar(50) NOT NULL' and Type 'MyTypes.EnumType' in Column 'EnumCol' of Type 'Table1' is not supported.
错误 1 DBML1005:不支持类型 'Table1' 的列 'EnumCol' 中的 DbType 'VarChar(50) NOT NULL' 和类型 'MyTypes.EnumType' 之间的映射。
This question: LINQ to SQL strings to enumsindicates that what I am trying to do is possible, but how is it done?
这个问题: LINQ to SQL strings to enums表明我想要做的事情是可能的,但它是如何完成的?
采纳答案by Marc Gravell
Curious - it should work IIRC; I'll see if I can do a quick example - however, you might want to check that you have the fully-qualified enum name (i.e. including the namespace).
好奇 - 它应该可以工作 IIRC;我会看看我是否可以做一个简单的例子 - 但是,您可能想要检查您是否拥有完全限定的枚举名称(即包括命名空间)。
[update] From hereit seems that the RTM version shipped with a bug when resolving the enum. One workaround suggested (on that page) was to add the global::
prefix. It works fine for me without this workaround, so maybe it is fixed in 3.5 SP1? It also allegedly works fine in 3.5 if you use the unqualified name if the enum is in the same namespace.
[更新] 从这里看来,RTM 版本在解析枚举时附带了一个错误。建议的一种解决方法(在该页面上)是添加global::
前缀。如果没有这个解决方法,它对我来说很好用,所以它可能在 3.5 SP1 中得到修复?据称,如果枚举位于同一命名空间中,则使用非限定名称在 3.5 中也能正常工作。
[example] Yup, worked fine: with Northwind, I defined an enum for the shipping country:
[示例] 是的,工作正常:使用 Northwind,我为运输国家/地区定义了一个枚举:
namespace Foo.Bar
{
public enum MyEnum
{
France,
Belgium,
Brazil,
Switzerland
}
}
I then edited the dbml to have:
然后我编辑了 dbml 以具有:
<Column Name="ShipCountry" Type="Foo.Bar.MyEnum" DbType="NVarChar(15)" CanBeNull="true" />
This generated:
这产生了:
private Foo.Bar.MyEnum _ShipCountry;
//...
[Column(Storage="_ShipCountry", DbType="NVarChar(15)", CanBeNull=true)]
public Foo.Bar.MyEnum ShipCountry
{ get {...} set {...} }
And finally wrote a query:
最后写了一个查询:
using (DataClasses1DataContext ctx = new DataClasses1DataContext())
{
var qry = from order in ctx.Orders
where order.ShipCountry == Foo.Bar.MyEnum.Brazil
|| order.ShipCountry == Foo.Bar.MyEnum.Belgium
select order;
foreach (var order in qry.Take(10))
{
Console.WriteLine("{0}, {1}", order.OrderID, order.ShipCountry);
}
}
Worked fine; results:
工作正常;结果:
10250, Brazil
10252, Belgium
10253, Brazil
10256, Brazil
10261, Brazil
10287, Brazil
10290, Brazil
10291, Brazil
10292, Brazil
10299, Brazil
回答by Pure.Krome
I know this has been answered, but i'm still getting this error also. Very weird.
我知道这已得到解答,但我仍然收到此错误。很奇怪。
Anyways, I found a solution. You need to PREPENDthe full namespace of the enum with global::
无论如何,我找到了解决方案。您需要预先准备枚举的完整命名空间global::
like WTF? Exactly. I know it sounds very weird. Here's an example screenie =>
喜欢WTF?确切地。我知道这听起来很奇怪。这是一个示例屏幕 =>
alt text http://img11.imageshack.us/img11/7517/lolzqg.png
替代文字 http://img11.imageshack.us/img11/7517/lolzqg.png
So lame :(
太蹩脚了:(
Anyways, I didn't figure this out. Some dude called Matt, did. And he posted a bug report on MS Connect and they can't repro it so it's not fixed, I guess.
无论如何,我没有弄清楚这一点。一个叫马特的家伙,做到了。他在 MS Connect 上发布了一个错误报告,他们无法复制它,所以我猜它没有修复。
Anyways, HTH.
无论如何,HTH。
回答by Manuel Castro
If you add the global:: qualyfier and press Control + space over the type in the designer.cs file it recognizes the type and you can remove it.
如果您添加 global:: 限定符并在 Designer.cs 文件中的类型上按 Control + 空格,它会识别该类型,您可以将其删除。