Question

I have a Dynamic Data Entities Web Application that uses a database with GUIDs as primary keys for tables. All of the GUID fields have the default value set to NEWID() so SQL Server will generate a new GUID for any record. However in my dynamic data web site, in the insert new entry pages the GUID field shows up and it is expected for the user to enter data. How could I prevent the field from being displayed?

Was it helpful?

Solution

According to MSDN:

"You can hide the primary key as you do any other column by using the Scaffold attribute and setting its value to false. Because automatically generated keys are not recognized, the default Insert page templates do not work with EDM for tables that have automatic primary keys unless you create a partial class for the entity and apply the Scaffold attribute with a value of false."

Also, as an aside it's preferable to use "newsequentialid()" for the default value of Guid if you're using SQL Server 2005 or higher as it's more efficient for inserting.

OTHER TIPS

Not the right Answer..

After doing this, which I did, you can insert 1 record which will get a GUID with all zero's. After that you will get no error message at all, but you will be unable to insert any more records, because of the unique index constraint on the guid ( which is a primary key of course ).

To really solve this problem, you have to edit the emdx file and add the

StoreGeneratedPattern="Identity"

into your Guid property tag under the "SSDL content" comment. Example:

   <Property Name="Guid" Type="uniqueidentifier" Nullable="false" StoreGeneratedPattern="Identity"/>

If yhou do this in the designer within visual studio it will annotate the property tag under the "CSDL content" comment which doesn't work..

Used Entity Framework 4.0 Visual studio 10 and .NET framework 4.0

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top