Aug 21 2007

LINQ To SQL and Allow Null in SQL Server 2005

Category: DLinq | LinqBil@l @ 21:06

I figured out tonight something new in LINQ To SQL related to updating records in the database and here it is:

Suppose you have a table in SQL Server 2005, that stores data related to Article. Each article has the ArticleID, Body, Abstract, title, etc ... Also, the Body column has AllowNull set to false, so it is a must to always have a value for the Body column.

When I open the web form used to update the article, and then I change the value of the Body field into "null", that is I simply delete the body value from the HTML Editor I am using (FCKEditor). Then i press Update, everything goes fine, but then i notice that the Body column in the table is still the old value and not the "null".

What happened here is that, LINQ To SQL will check if the value for a column, who has AllowNull is false, is null, then the old value of the column will be preserved. However, if I update the value of the Body field to another value different from null, then the Body column is being updated sucessfully!!

Hope this feature helps you out while working on LINQ To SQL.


Tags: ,

Comments are closed