Search code examples
linqlinq-to-sqlpartial-methods

How to code the partial extensions that Linq to SQL autogenerates?


I made a class from Linq to SQL Clasees with VS 2008 SP1 Framework 3.5 SP1, in this case I extended the partial

partial void UpdateMyTable(MyTable instance){
   // Business logic
   // Validation rules, etc.
}

My problem is when I execute db.SubmitChanges(), it executes UpdateMyTable and makes the validations but it doesn't update, I get this error:

[Exception: Deliver]
   System.Data.Linq.ChangeProcessor.SendOnValidate(MetaType type, TrackedObject item, ChangeAction changeAction) +197
   System.Data.Linq.ChangeProcessor.ValidateAll(IEnumerable`1 list) +255
   System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode) +76
   System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) +331
   System.Data.Linq.DataContext.SubmitChanges() +19

Solution

    • if you provide this method, you must perform the update in the method.

    http://msdn.microsoft.com/en-us/library/bb882671.aspx

    • If you implement the Insert, Update and Delete methods in your partial class, the LINQ to SQL runtime will call them instead of its own default methods when SubmitChanges is called.

    Try MiTabla.OnValidate