Geeks With Blogs

Steve Clements .Net and then some....

I have recently came up against a perfect opportunity to get stuck into LINQ to SQL, its a small project where RAD is top priority.  As often is the case I need to audit changes to properties, normally I would probably do some work in the getter and setter of the property, but of course with LINQ to SQL I don't have access to the properties in the same way.  Looking through the intellisense of the LINQ generated class I can see a PropertyChanged and PropertyChanging event, perfect!  Not exactly.  Here is what is generated for you...

The event logic...


The property getter/setter...


Back to my code...

As you can see below there are two arguments, as normal! and the PropertyChangingEventArgs has a PropertyName property.  Two problems I have with this; 1. PropertyName is only set on the PropertyChanged event, not PropertyChanging, 2. Is that it? The property name, what about original and current value for starters? Which is exactly what is needed for an audit log.


Well you could cast out the sender.


Cool, you now have the class, with your new value but the class doesn't tell you which property has changed, wait, you have the property name, but as a string - yuk!! So in case you haven't figured out where I am going with this, you will need to do some manual and frankly nasty processing in the property changing event to get the the original value and some more manual processing the property changed event to get the current value.  Don't forget that the casting out the sender in the PropertyChanging handler will not be any use as its a reference type and will refer to the same class after the change has happened, so you will have two variables with a reference to the same instance.

All of this turned out the be a complete waster of thinking as on further investigation I found the GetModifiedMembers method in the "LINQ table" collection, that takes an argument of the type and returns an array of System.Data.Linq.ModifiedMemberInfo objects.  Here you can see...


Perfect... :)

.net I salute you!! There is always a way and boy, teasing it out of you is such fun :)

Posted on Tuesday, April 15, 2008 5:39 PM .NET 3.5 , LINQ | Back to top

Comments on this post: LINQ to SQL: Property Changed / Changing Logging

# re: LINQ to SQL: Property Changed / Changing Logging
Requesting Gravatar...
you saved me a lot of time, mate
Left by Doctor Sid on Dec 22, 2009 1:06 AM

# re: LINQ to SQL: Property Changed / Changing Logging
Requesting Gravatar...
Where is "context" comming from?
Left by Chrys on Nov 29, 2011 4:37 AM

Your comment:
 (will show your gravatar)

Copyright © Steve Clements | Powered by: