I’ve been recently quite surprised to learn that there is no way to tell whether an entity property in LINQ to SQL is updated or not. Well I’d agree that in majority of situations there is no need to know it because your database context object will take care of all properties and submit to the database only those which were updated. While it’s true there are cases when it’s necessary to know which properties are updated before you pass them on to the database context for submission. For example in ASP.NET MVC  there might be HTTP POST actions which accept your model object as an action parameter (a fantastic feature of ASP.NET MVC) and populate it with your form data. Then in your action code you may want to know which properties of the model entity are modified by MVC.

There is a reasonably quick and reliable method here to help you out. Just create a partial class for the entity of concern and make use of the PropertyChanged event of LINQ to SQL entities. This is an example code:


public partial class Quote
{
  public Dictionary ModifiedProperties =
  new Dictionary();
 
  partial void OnCreated()
  {
      this.PropertyChanged += HandlePropertyChangedEvent;
   }
 
   public void HandlePropertyChangedEvent(object sender, PropertyChangedEventArgs a)
    {
        ModifiedProperties[a.PropertyName] = true;
    }
}

Then to check your Quote object properties just check its ModifiedProperties collection.

You may also like

SharePoint Calendar view with date formatting

On one of our recent SharePoint implementations our client, like most, wanted to take advantage of the calendar functionality and list the next 5 events on the homepage. This was very simple to do but the issue we encountered was on the homepage. Whenever a full day event was displayed it was showing a start time of 12am.

Keep Reading

Parsing CSV files in .NET

Just in the last 2 weeks I happened to write 2 custom CSV parsers for different projects. With the last one when I “almost” finished I realised I just cannot go on like that any more - I’m sick and tired reinventing the wheel. The purpose of this blog is to spread the word about a great CSV parser I found: www.filehelpers.com

Keep Reading

Newsletter sign up

Every couple of months we send out an update on what's been happening around our office and the web. Sign up and see what you think. And of course, we never spam.