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

2 Comments