How to change update statement before execution: Linq2Sql Classes

I have applied change tracking (http://msdn.microsoft.com/en-us/library/cc280462.aspx) in some tables in which I use Linq2Sql.

As part of this, I need to add the following SQL to the top of the generated update statements.

DECLARE @originator_id varbinary(128); SET @originator_id = CAST('SyncService' AS varbinary(128)); WITH CHANGE_TRACKING_CONTEXT (@originator_id) ....generated statements.... .... .... 

I know that I can create stored procedures and manually display fields, but I would like to avoid this if possible.

Does anyone know a way to override and edit SQL on SubmitChanges ()?

+4
source share
1 answer

You can override the Update method by running partial classes in your datacontext, which will instead call LINQ to SQL. Just give it a signature:

partial void UpdateClassName(ClassName instance)

You can also go on to what he usually used using:

ExecuteDynamicInsert(instance);

Unfortunately, there is no mechanism just to return the requested SQL for insert / update / delete (you can get SELECT statements with GetCommand in the DataContext)

+1
source

All Articles