Another way to update specific columns of a database record in Linq to SQL is use the ExecuteCommand method to execute any SQL statement in the database, bypassing Linq altogether:
using (var database = new DataContext()) { database.ExecuteCommand( "UPDATE FooMax SET ID = {1} WHERE OID = {0}", OID, newID); database.SubmitChanges(); }
The advantage of this solution is that ExecuteCommand allows you to
- dynamically create SQL statements
- send the most complex statements to the database which might not easily be accomplished using Linq
The major drawback of the solution is that it shoots a big hole in your OR mapper and layered architecture:
- Your C# code contains pieces of SQL statements as unverifiable strings
- Changes in the database model may break existing C# code, noticed only during execution
- Database abstraction is lost