Partial Methods on a Linq DataContext

Following on from my previous post and my connect feedback I thought I would write about the partial methods on a linq datacontext.

As I thought, Dinesh confirmed that the partial methods (Insert, Update, Delete) on a DataContext are overriding instead of notifications.  The reason for this is so that you can override the default logic, so you could redirect the update via a stored procedure.

However, you can continue processing the default way, even within the partial method by calling this.ExecuteDynamicXXXXX() method.

For example, the method below overrides the default UpdateOrder logic, writes out information to the console then continues to execute the update by calling ExecuteDynamicUpdate();

partial class DataClasses1DataContext
{
partial void UpdateOrder(Order instance)
{
Console.WriteLine(“UpdateOrder”);
this.ExecuteDynamicUpdate(instance);
}
}

This is definitely something to be aware of, but the new methods means that it is not a problem if you want to include custom logic but still process the command in the same way.

4 thoughts on “Partial Methods on a Linq DataContext”

  1. Great, I assumed it was a notification and couldnt get it to work as I wanted it too ( I needed notification on the context level)

Leave a Reply

Your email address will not be published. Required fields are marked *