Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

Let's say you have a table in the database named Customers. One of the field of the Customers table is DateModified. The default for the DateModified is "GETDATE()" T-SQL Function which means if you don't provide anything it will default to the current date and time. Off course, this only works when you are inserting the record for the first time. And that's the way it should be.

Now, if you are using LINQ to SQL to insert the data into the database and you want the default value to be generated then you will need to set the "Auto Generated Value" property to true. You can find this property by right clicking on the field of the table which has been dropped on the LINQ to SQL designer. Now, when the new Customer is inserted into the database the DateModified value will be automatically generated by LINQ. Everything is good! Later you decided to update the Customer information and change the DateModified field. LINQ to SQL will through an exception when you assign any value to the DateModified field. The exception will tell you that you cannot assign anything to DateModified field since you have put it as an Autogenerated Value.

This is a problem! LINQ to SQL should allow the developer to change the DateModified even if the property says Autogenerated Value. If this is not the case then we have to set the Autogenerated Value to false and manually insert it even if the record is new.

Hopefully, the LINQ guys will read this post and make the necessary changes to remove this big problem.

Posted on Thursday, March 13, 2008 5:13 AM | Back to top


Comments on this post: LINQ to SQL Auto Generated Value Problems!

# re: LINQ to SQL Auto Generated Value Problems!
Requesting Gravatar...
In my opinion they should also pick up if the column is "auto generated".

I have stopped having columns with auto values because of this behaviour.

Best regards,
Stigur
Left by Stigur on Mar 13, 2008 6:38 PM

# re: LINQ to SQL Auto Generated Value Problems!
Requesting Gravatar...
Hi azam sorry my problem is not related to this topic my problem is different,
i tried to define constraint pk_empid on a particular table but its give me an error saying that 'There is an already an object with this name. i can create constraint with other name but i want to find out on which table this constraint is created how can i do this..
Left by mujtaba faizan on Mar 14, 2008 1:01 AM

# re: LINQ to SQL Auto Generated Value Problems!
Requesting Gravatar...
Hi Stigur,

I agree!
Left by Mohammad Azam on Mar 15, 2008 8:00 AM

# re: LINQ to SQL Auto Generated Value Problems!
Requesting Gravatar...
I agree with you totally. I think it should be obvious that if the primary key is null in the object you're trying to 'InsertOnSubmit()'. Then LINQ should know to generate the ID for you - No matter what the 'Autogenerate' is set to.
Left by John Phee on May 06, 2008 4:29 AM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net