.Net – issues using uniqieidentifier as a primary key in VS2005

โ€”

by

in

Ok. First Visual Studio 2005 bug, complete with my work around. Now, this bug is apparently known and a few semi-workarounds exist but they all have issues not the least of which is hand editing of my nice shiny new generated code and other crap.

The problem: It seems that Visual Studio will totally fail to correctly deal with tables that use a uniqueidentifier column type as their primary key. And yes, that sucks deeply and hugely.

Assuming you absolutely need a guid associated with each record (and there are good reasons for this) it turns out after some reflection you almost certainly do not need it to be the primary key for that table. You can let VS2005 have it’s integer based identity column and simply give yourself a nice little guid column for your own future use.

Your happy, VS2005 is happy, your records have a guid and life, as they say, is good.


Comments

2 responses to “.Net – issues using uniqieidentifier as a primary key in VS2005”

  1. Nmyvision

    I’m using Sql2005 so not sure if this works with 2000 but here is what i did to get it working:

    change the parameter in your stored proc from uniqueidentifier to sql_variant “@UID sql_variant” and in the code where u actually need the guid us the Convert function : “Convert(uniqueidentifier , @UID),”

  2. Nice workaround, thanks for posting it!