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.
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).
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!
Leave a comment