Handling Default Values With LINQ to SQL
Hi All,
Have run into some issues with using default values in my SQL database with LINQ. I set AutoGenerated in the LINQ Designer to true which made use of my default values but this did not let me update this manually as it would throw an exception. I got a good suggestion from Young Joo to keep autogenerated set to false and provide my default values on the client. To do this I implemented the OnCreated partial method and set up a default value for my item.
I.E.
partial void OnCreated() {
if (this.DateTimeCreated == null) {
this.DateTimeCreated = DateTime.Now;
}
}
This will setupt the default value for our object each time we create an instance of it. You will then either override the value when you load from the database, or when you change the values before an insert/update. This solved my issue with handling default values and seems to work quite well.
Thanks
Stefan