Saturday, July 28, 2007

LINQ to SQL enum mapping

UPDATE: Figured this one out! I was always using a fully-qualified type name on my enums- for this to work, you have to prepend the global:: prefix. Unqualified type names work fine, as long as the type is visible from the generated LINQ entity class namespace. I filed this as a bug...

-----

Has anyone successfully managed to use the Orcas Beta2 (sorry, VS2008) bits to do first-class enum mapping onto an int column? My Beta1 code is doing the "define another property that does the mapping from int->enum and back" trick, but I'd like to let the mapper do it for me now that it's supposedly supported. I've got my enum defined, but when I set the 'Type' field on the column properties in the O/R designer to the FQ name of the enum, I get the following error:

DBML1005: Mapping between DbType 'Int' and Type 'AnApp.AnEnum' in Column 'EnumDBColumn' of Type 'ATable' is not supported.

I know it's finding the type- I get a different error if I use a bogus type name. Grr. I'm hoping I'm just doing something dumb- I really want this to work!

13 comments:

Anonymous said...

Hi,
Did you end up filing a bug for this?

Matt Davis said...

I did (https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=289435), but after fighting with the Connect moderator for a couple of rounds (and even giving them a sample app), I gave up. Some groups have really sharp moderators watching their Connect postings, some don't. I had a workaround, and too much work to do on my own product to keep fighting with them- I believe they "Closed, Not Reproduced" the issue. Ha!

Anonymous said...

Works for me, if I just set the enum's type name, instead of the FullName.

Matt Davis said...

You missed the point- setting the non-namespace-qualified type name works fine if the enum is in the same namespace as the DataContext/Entity types. It breaks when the enum is in a different namespace.

Anonymous said...

Broken keyboard on this bug. Thank you.

Mark Smeltzer said...

This bug still exists in the release version of VS 2008. Thanks for posting a fix!

Anonymous said...

Thanks Matt for the fix, for me I haven't set a namespace for the DataContext/Entity types so it should be the same as my enum types. However, although it works most of the time intermittently I get this error and find I have to either restart visual studio or delete the linq class file and recreate. But your workaround solved the issue for me.

Haider said...

Thanks a lot for the fix mate. Wonder how you came up with "global::" prefix :)

Funka! said...

Your nearly-two-year-old post has just saved my day: it appears that still today, in June 2009, we have same problem in Visual Studio 2008 SP1! Thanks for the workaround!

Anonymous said...

Awesome. You saved me!!

Anonymous said...

My project was building fine but suddenly that DBML1005 error appeared after making an unrelated change to the dbml file. Prepending the global:: namespace fixed the issue.

Thanks!!!

Anonymous said...

Your post is still helping - it was the fix to my error as well. Thanks!

Gareth Thomas Hill said...

global:: helped me too - VS2010 RC with Framework 4...

Thanks for this!