Thursday
Dec112008
ASP.NET Dynamic Data - GUIDs
Eric Phan |
Thursday 11 December 2008 at 07:47 AM 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
- In the DBML Designer, select the GUID primary key and select Properties
- Set the Auto Generated Value property to True

- 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
- 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
tagged
ASP.NET Dynamic Data in
Development
ASP.NET Dynamic Data in
Development 
Reader Comments (2)
what about linq to entities?
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.