LINQ to SQL Auto Generated Value Problems!

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.

Print | posted @ Thursday, March 13, 2008 5:13 AM

Twitter