映射枚举从字符串

映射枚举从字符串

本文介绍了映射枚举从字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有其中映射到枚举在代码数据库表字符串列。在我的dbml文件时,我设置的类型来MyTypes.EnumType我收到以下错误:

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:

错误1 DBML1005:DbType之的VarChar之间的映射(50)NOT NULL '型和'MyTypes.EnumType栏目中的表1不支持'类型'EnumCol

Error 1DBML1005: Mapping between DbType 'VarChar(50) NOT NULL' and Type 'MyTypes.EnumType' in Column 'EnumCol' of Type 'Table1' is not supported.

这问题:
的的似乎RT​​M版本附带有解决枚举时错误。建议(即页)一个解决方法是添加全球:: 前缀。它工作正常,我没有这种解决方法,所以也许它是固定在3.5 SP1?如果你使用不合格的名称,如果枚举是在同一个命名空间还涉嫌正常工作在3.5

[update] From here it 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.

【示例】是啊,工作得很好:与罗斯文,我定义为航运国家的枚举:

[example] Yup, worked fine: with Northwind, I defined an enum for the shipping country:

namespace Foo.Bar
{
    public enum MyEnum
    {
        France,
        Belgium,
        Brazil,
        Switzerland
    }
}

然后我编辑的dbml的有:

I then edited the dbml to have:

<Column Name="ShipCountry" Type="Foo.Bar.MyEnum" DbType="NVarChar(15)" CanBeNull="true" />

这生成的:

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

这篇关于映射枚举从字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 05:15