views:

305

answers:

1

I am running into a problem where my CRUD operations on an entity sourced from an SQL View is not calling the generated methods for said operations.

Example:

I press "Delete" in a ListView on an item, connected to a LinqDataSource. It throws an error saying that it cannot perform the operation because it affects multiple base tables. That's fine, I understand that. What I don't understand is why this code won't run on insert/delete:

Public Partial Class Entity

Private Sub DeleteEntity(instance as Entity)
    Throw New Exception("TEST")
End Sub

End Class

In debug, it won't break on the method, so it's not being called. I even did a test where I deleted the entity by attaching/DeleteOnSubmit and still no-go. Is this a bug or am I not handling the right method?

Note: Yes, I can handle a data source's OnDeleting event, cancel, etc. (which is my temporary fix) but I'd really like to catch ALL delete operations in a central place no matter how I delete the entity.

A: 

There should be a partial method on your entity called OnValidate(System.Data.Linq.ChangeAction action). Handling that may be what you're looking for.

Private Partial Sub OnValidate(action As System.Data.Linq.ChangeAction)
    If action = System.Data.Linq.ChangeAction.Delete
        Throw New Exception("TEST")
    End If
End Sub
Jacob Proffitt
Thanks, this works, albeit not as great as overriding the different CRUD actions would be. It'll do, I think.
subkamran
Yeah, it'd be nicer not to have to test the ChangeAction, but this was the closest I could find that'd work more or less the way you'd like it to.
Jacob Proffitt
I also ended up using custom stored procedures doing CUD operations since that worked better and also handled those operations from both LinqDataSource controls and programmatic operations.
subkamran