Running with Code Like with scissors, only more dangerous


Using C# Enumerations as LINQ-to-SQL Entity Properties

Have you ever created a database object and said "OK, this column is going to correspond to this enumeration"?  If you’re obsessive like me, you might have even gone so far as to create column restrictions for the valid range of values, and created a special SQL type that doesn’t really do anything except give you peace of mind.

Well, I’ve got about three such fields on a couple entities on a recent project.  Since I wanted those properties to go into C# enumerations, I tried the natural thing: I typed the enumeration’s type name into the "Type" property.

Setting a column's Type property.

Unfortunately, doing this didn’t work.  In fact, it seemed to break Visual Studio; updates stopped propagating to my LINQ-to-SQL classes, and in fact since I had done this before a single save, I didn’t get any entity classes. 

It turns out that Matt Davis found the answer to the problem: qualify the enumeration’s type name with the global:: namespace qualifier if it doesn’t live in a namespace (for instance, code within an ASP.NET App_Code folder).

The global:: qualifier is important.

Once I added the qualifier to my type names, saving the DBML file correctly updated the LINQ-to-SQL classes, and I was off and running!

Comments (0) Trackbacks (0)

No comments yet.

Leave a comment

ERROR: si-captcha.php plugin says GD image support not detected in PHP!

Contact your web host and ask them why GD image support is not enabled for PHP.

ERROR: si-captcha.php plugin says imagepng function not detected in PHP!

Contact your web host and ask them why imagepng function is not enabled for PHP.

No trackbacks yet.