views:

403

answers:

1

Lets say I have a couple Linq to SQL classes, Person and Department. Person has a entity association to Department.

I can catch a change to Person.Name using OnNameChanging and OnNameChanged in the partial class but the designer generated code doesn't seem to call OnDepartmentChanging or OnDepartmentChanged. How can I catch these kind of changes?

+1  A: 

Would it be an option to make the property private (select Code property in the dbml and set the access property in the Properties window) and create a new public property over which you have more control? This property can calls the private property and does the event raising.

I personally have have written a generator that generates the necessary files for me (like sqlmetal), giving me full control over the code. Perhaps this is an option for you as well, if you do not like the generated dbml.

This is btw almost exactly the same answer as I gave to this question: http://stackoverflow.com/questions/522172/can-you-prevent-linqdatasource-from-setting-a-property/522478#522478

I hope it helps.

Florian
Potentially I could do that, in fact I think the LLBLGEN Linq to SQL designer code might let you do that. If so you obviously need to drop the MS designers (which should have had more options for the generated code).
Christopher Edwards
But I feel like there may be a better way of doing it using the existing code gen/partial class model. The Trouble with partial classes is that you can't "override" properties. Maybe a derived class could do better.
Christopher Edwards
Before we had our own generator, I made the datacontext indeed abstract and then use a derived base class. From memory I believe this is not possible with classes. If it is possible, then that would be an option. But it would in fact be the same as the solution described in my answer.
Florian
Btw: if you are working on a big project, have a look at external mapping as well.
Florian