Request a topic or
Contact an Arke consultant
404-812-3123
LinqDataSource with uniqueidentifier Primary Key

Arke Systems Blog

Useful technical and business information straight from Arke.

About the author

Author Name is someone.
E-mail me Send mail

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2009

LinqDataSource with uniqueidentifier Primary Key

I tend to use uniqueidentifiers for my primary key columns with a default value of newid() so the database will generate a Guid for me.  I created a LinqDataSource and bound that to a DetailsView control.  I was able to insert my first row within 10 minutes of writing all this code but subsequent inserts would always throw a unique constraint exception.  After looking at the database, I saw my first row's Id was Guid.Empty so I knew that Linq was pushing this as a default value to the database instead of letting the database create a newid() for me.  It turns out the solution is very simple.  Open your DBML file, select the primary key column and change the Auto Generated Value property to true.  So far, I'm still impressed with Linq.

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: LINQ
Posted by Eric Stoll on Sunday, January 20, 2008 2:18 PM
Permalink | Comments (5) | Post RSSRSS comment feed

Related posts

Comments

Gert T be

Saturday, April 26, 2008 10:41 AM

Gert T

As far as i know there is no 'Auto generated Value' property in MS-SQL 2005 express.
'default value or binding' is (newid()) but that didn't work when inserting data with LINQ.
So I just used System.Guid.NewGuid to generate the unique identifier like this:

Example in VB.Net (VS2008)
Dim newContactType As New ContactType
newContactType.Id = System.Guid.NewGuid
newContactType.Type = sContactType
db.ContactTypes.InsertOnSubmit(newContactType)
db.SubmitChanges()

Eric Stoll us

Sunday, May 04, 2008 2:49 PM

Eric Stoll

When you set Auto Generated Value to true, LINQ leaves it up to the database to set the default value. So while the instance of your object is new and not committed, the PK value is Guid.Empty. When you SubmitChanges() the database creates a default value and your LINQ object will have the ID from there out. However, you might want the option to set the default value from code as well. In that case, with Auto Generated Value turned on, if you set the ID from code, the database will still create a new default value when you SubmitChanges. This is obviously not a good scenario because the database won't have the ID you intended to set. So I found a way around this... Instead of setting Auto Generated Value to true, add the following partial method to your class.

partial void OnCreated() { Id = Guid.NewGuid(); }

That's it! This will force LINQ to create a default value right when an instance of the object is created. You will have access to the right ID value from your code before and after committing it to the database!

Eric Stoll us

Sunday, May 04, 2008 2:59 PM

Eric Stoll

If you aren't very familiar with partial methods yet this is a good article. blogs.msdn.com/.../in-case-you-haven-t-heard.aspx

Busby SEO Test Pinay us

Saturday, December 27, 2008 12:26 AM

Busby SEO Test Pinay

Nice and great blog.

Busby SEO Test us

Monday, December 29, 2008 6:49 AM

Busby SEO Test

This is really educative

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Wednesday, January 07, 2009 1:37 AM