Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping Enum from String

Tags:

c#

linq-to-sql

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:

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

This question: LINQ to SQL strings to enums indicates that what I am trying to do is possible, but how is it done?

like image 804
Barry Avatar asked Oct 17 '08 12:10

Barry


People also ask

How do I convert string to enum?

Use the Enum. IsDefined() method to check if a given string name or integer value is defined in a specified enumeration. Thus, the conversion of String to Enum can be implemented using the Enum. Parse ( ) and Enum.

How do I create an enum of a string in C++?

Here we will see how to map some enum type data to a string in C++. There is no such direct function to do so. But we can create our own function to convert enum to string. We shall create a function that takes an enum value as the argument, and we manually return the enum names as a string from that function.

Can we convert string to enum in Java?

You can create Enum from String by using Enum. valueOf() method. valueOf() is a static method that is added on every Enum class during compile-time and it's implicitly available to all Enum along with values(), name(), and cardinal() methods.


2 Answers

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).

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

I then edited the dbml to have:

<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 
like image 156
Marc Gravell Avatar answered Sep 25 '22 03:09

Marc Gravell


I know this has been answered, but i'm still getting this error also. Very weird.

Anyways, I found a solution. You need to PREPEND the full namespace of the enum with global::

like WTF? Exactly. I know it sounds very weird. Here's an example screenie =>

alt text 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.

Anyways, HTH.

like image 21
Pure.Krome Avatar answered Sep 26 '22 03:09

Pure.Krome