Connect with me
Search
Twitter Feed
Navigation
« Cross browser copy and paste - with jQuery.Copy | Main | Enable your incompatible extensions in Firefox 3.1 betas »
Thursday
Dec112008

ASP.NET Dynamic Data - GUIDs

Dynamic Data by default doesn’t play well with GUID primary keys, or more correctly LINQ to SQL doesn’t auto generate the GUIDs for you by default. You will get validation errors when you try to insert

Note: the Entity Framework also suffers from this problem

You have two options here:

Option 1 - Set the Auto Generate Property to True

  1. In the DBML Designer, select the GUID primary key and select Properties
  2. Set the Auto Generated Value property to True
  3. Now the UserID field is hidden in the insert page and we are able to successfully insert

The problem with this method is that every time the DBML file gets regenerated you lose this change and you get the validation error again. For a more permanent solution you can try option 2

Option 2 - Turn Scaffolding off through metadata

  1. Create a metadata class and add the ScaffoldColumn(false) attribute to the UserID field
	using System.ComponentModel.DataAnnotations;

	[MetadataType(typeof(UserMetadata)]
	public partial class User { }

	public partial class UserMetadata
	{
		[ScaffoldColumn(false)]
		public object UserID { get; set; }
	}

This gives you a more permanent solution that can survive regenerating your DBML

Reader Comments (2)

what about linq to entities?

20 March 2010 | Unregistered Commentersofter

So, this really doesn't solve the problem, since linq doesn't auto generate a new GUID for you. This only handles the web UI problem with Scaffolding.

20 March 2010 | Unregistered Commenterzam

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>