none
Newbie: Sql Column with a default value set to (newid())

    Question

  • Hello,

    Thanks for reviewing my question.  I have a table with a column (set as the primary key) with a default value as (newid()).  Now, since I am using Linq to Sql, when I create a new record in my code, shouldn't that column now be set to some GUID?

    I ran through my code and checked the database and saw that my column was set to 0000-0000-... and not unique identifier.  However, if I enter a new record via SQL server, the column gets assigned a GUID.

    What's up with that?

    Do I have do something like this in my C# code?   vol.guid = System.Guid.NewGuid();

    Many Thanks

    Peter

    Tuesday, April 19, 2011 7:17 PM

Answers

  •  

    If you have an identity column set then you can do as you say and create the value using NewGuid().

    Using LINQ to SPROC you can write an SPROC that will let the db assign the Guid if you desire it to be done at the DB, although this shouldn't be necessary.

    There is a property in the linq designer table column called "Auto Generated Value" check that it is set to true for that column.

    Perhaps your dbml is not up to date because I simply can't remember having this issue but I feel I generally create the Guid in my code when using Guids since they cannot clash anyway.

     

     


    Allan Merolla | BEng, JD, MCP | .NET/Sharepoint | My Blog at http://www.parallelfun.com/
    • Marked as answer by Peter Konstant Wednesday, April 20, 2011 5:42 PM
    Wednesday, April 20, 2011 12:38 AM
  • Hello Peter,

    In the Linq to Sql designer, set the Auto Generated Value property to true for that column. This is equivalent to the IsDbGenerated property. The only limitation is that you can't update the value using Linq.

    Kind regards,


    aelassas.free.fr
    • Marked as answer by Peter Konstant Wednesday, April 20, 2011 5:41 PM
    Wednesday, April 20, 2011 10:03 AM

All replies

  •  

    If you have an identity column set then you can do as you say and create the value using NewGuid().

    Using LINQ to SPROC you can write an SPROC that will let the db assign the Guid if you desire it to be done at the DB, although this shouldn't be necessary.

    There is a property in the linq designer table column called "Auto Generated Value" check that it is set to true for that column.

    Perhaps your dbml is not up to date because I simply can't remember having this issue but I feel I generally create the Guid in my code when using Guids since they cannot clash anyway.

     

     


    Allan Merolla | BEng, JD, MCP | .NET/Sharepoint | My Blog at http://www.parallelfun.com/
    • Marked as answer by Peter Konstant Wednesday, April 20, 2011 5:42 PM
    Wednesday, April 20, 2011 12:38 AM
  • Hello Peter,

    In the Linq to Sql designer, set the Auto Generated Value property to true for that column. This is equivalent to the IsDbGenerated property. The only limitation is that you can't update the value using Linq.

    Kind regards,


    aelassas.free.fr
    • Marked as answer by Peter Konstant Wednesday, April 20, 2011 5:41 PM
    Wednesday, April 20, 2011 10:03 AM