Dynamic Data treats Primary Keys in a special way (for example, hiding them from the autogenerated pages). Having an autogenerated GUID actually falls within the realm of SQL Server. You can achieve this by specifying that your GUID column should have a
default value and that default value should be ther result of evaluating the newid() function (look at Example B here for more info:
http://msdn2.microsoft.com/en-us/library/ms190348.aspx).
You can set that property using the Linq to SQL designer and then it will automatically be included in the autogenerated code (unless you completely remove a table from your model and drag it in again - but this is an issue with Linq to SQL and not Dynamic
Data):
<div mce_keep="true">Open your Linq to SQL model</div>
<div mce_keep="true">Right-click on your GUID property and select Properties</div>
<div mce_keep="true">In the Properties Editor in the Data Section change the value of the "Auto Generated Value" property to true.</div>
I have a similar problem using an auto-incrementing numeric as a primary key.
When adding a new item using the default template it creates a textbox for the ID.
Hiding the column is no good as it will complain that the column is required.
Whatever number you type in it will save the next available ID anyway, unless you type an existing ID, in which case it will complain the ID already exists.
IsDbGenerated is set to true.
Is there an easy, or even a dirty way around this?
Hide the ID column and pass the max possible ID every time somehow?
First off I'm assuming that the field in your database is set as an identity column? If it is an identity column and a primary key then we will hide the field and it will automatically get incremented as each new row is put in. If you modified the database
after you created your Linq Classes you probably should delete the table from the Linq designer and drag it back onto the designer from the database. On my machine a working table with an identity primary key looks like this:
Auto Generated Value: true
Auto-Sync: OnIsert
Primary Key: true
Note if we do not show a column and you want to add it you can always for the column to show by adding it manually:
Sorry Scott, only feeling my way around these controls at the moment.
Found the Fields collection in the DynamicDetailsView so I can omit the offending column when new is clicked cheers.
If the boss decides they want the ID field displayed too I'll use renderhints to point to a custom _Edit Dynamic Data field that just displays the value in a literal and shows "New" if the value is blank.
Note that if you have a DynamicDetailsView, you are likely using the old December build, as this control no longer exists. If possible, please give a try to the
new build.
Ig0r
Member
5 Points
5 Posts
ASP.NET Dynamic Data + guid
Dec 11, 2007 06:50 PM|LINK
Hi,
I use guid as my primary key.
Insert command don't create new guid when i insert record.
Is it possible to customize guid data filelds??
Thanks
Igor
marcind
Contributor
3248 Points
591 Posts
AspNetTeam
Microsoft
Re: ASP.NET Dynamic Data + guid
Dec 11, 2007 11:38 PM|LINK
Hi Igor,
Dynamic Data treats Primary Keys in a special way (for example, hiding them from the autogenerated pages). Having an autogenerated GUID actually falls within the realm of SQL Server. You can achieve this by specifying that your GUID column should have a default value and that default value should be ther result of evaluating the newid() function (look at Example B here for more info: http://msdn2.microsoft.com/en-us/library/ms190348.aspx).
ASP.NET Team
@marcind
Blog
Ig0r
Member
5 Points
5 Posts
Re: ASP.NET Dynamic Data + guid
Dec 12, 2007 08:42 AM|LINK
Hi Marcin,
Thanks for the quick answer.
Best Regards
Igor
Ig0r
Member
5 Points
5 Posts
Re: ASP.NET Dynamic Data + guid
Dec 12, 2007 11:06 AM|LINK
I still have the same problem after adding newid() function.
I seems that linq to sql always insert 00000000-0000-0000-0000-000000000000 value in db.
Any ideas?
Thanks
Ig0r
Member
5 Points
5 Posts
Re: ASP.NET Dynamic Data + guid
Dec 12, 2007 11:31 AM|LINK
One solution is to add newid() function in db and than change column property isDbGenerated:=True in linq to sql generated classes
<Column(Storage:=
"_CustomerFaxTypeID", DbType:="UniqueIdentifier NOT NULL", IsPrimaryKey:=True, IsDbGenerated:=True)> _Problem is that i must change that property every time when i generate linq to sql classes.
Is there any other solution??
Igor
marcind
Contributor
3248 Points
591 Posts
AspNetTeam
Microsoft
Re: ASP.NET Dynamic Data + guid
Dec 12, 2007 05:04 PM|LINK
Sorry, I forgot to mention that detail.
You can set that property using the Linq to SQL designer and then it will automatically be included in the autogenerated code (unless you completely remove a table from your model and drag it in again - but this is an issue with Linq to SQL and not Dynamic Data):
ASP.NET Team
@marcind
Blog
TonyHawe
Member
4 Points
4 Posts
Re: ASP.NET Dynamic Data + guid
Apr 14, 2008 09:43 PM|LINK
I have a similar problem using an auto-incrementing numeric as a primary key.
When adding a new item using the default template it creates a textbox for the ID.
Hiding the column is no good as it will complain that the column is required.
Whatever number you type in it will save the next available ID anyway, unless you type an existing ID, in which case it will complain the ID already exists.
IsDbGenerated is set to true.
Is there an easy, or even a dirty way around this?
Hide the ID column and pass the max possible ID every time somehow?
scothu
Participant
1424 Points
290 Posts
AspNetTeam
Microsoft
Moderator
Re: ASP.NET Dynamic Data + guid
Apr 14, 2008 11:00 PM|LINK
First off I'm assuming that the field in your database is set as an identity column? If it is an identity column and a primary key then we will hide the field and it will automatically get incremented as each new row is put in. If you modified the database after you created your Linq Classes you probably should delete the table from the Linq designer and drag it back onto the designer from the database. On my machine a working table with an identity primary key looks like this:
Auto Generated Value: true
Auto-Sync: OnIsert
Primary Key: true
Note if we do not show a column and you want to add it you can always for the column to show by adding it manually:
<asp:DynamicField DataField="fieldname" />
...Scott
PM, ASP.NET Team, Microsoft
TonyHawe
Member
4 Points
4 Posts
Re: ASP.NET Dynamic Data + guid
Apr 15, 2008 12:04 AM|LINK
Sorry Scott, only feeling my way around these controls at the moment.
Found the Fields collection in the DynamicDetailsView so I can omit the offending column when new is clicked cheers.
If the boss decides they want the ID field displayed too I'll use renderhints to point to a custom _Edit Dynamic Data field that just displays the value in a literal and shows "New" if the value is blank.
Cheers for the speedy response.
davidebb
Contributor
6960 Points
1358 Posts
AspNetTeam
Re: ASP.NET Dynamic Data + guid
Apr 15, 2008 12:24 AM|LINK
Note that if you have a DynamicDetailsView, you are likely using the old December build, as this control no longer exists. If possible, please give a try to the new build.
thanks,
David