Solved

Determining if ObjectStateManager has dirty records

Posted on 2010-11-17
3
1,349 Views
Last Modified: 2012-05-10
I would like a sanity check.

Here is a static method I wrote to determine if the .Net Framework Entity's ObjectStateManager has any dirty records. (This code would be used to determine the state of the Save button, namely, count>0 means enabled else disabled.)

Is there no easier way to do this? Does ObjectStateManager not have any kind of a property to flag whether its contents are dirty?
static public int ChangedCount(this ObjectStateManager manager)
    {
        int changedCount = 0;
        try
        {
            changedCount += manager.GetObjectStateEntries(EntityState.Added).Count<ObjectStateEntry>();
        }
        catch {}

        try
        {
            changedCount += manager.GetObjectStateEntries(EntityState.Deleted).Count<ObjectStateEntry>();
         }
         catch { }
         
         try
         {
            changedCount += manager.GetObjectStateEntries(EntityState.Modified).Count<ObjectStateEntry>();
         }
         catch { }
            
         return changedCount;
    }

Open in new window

0
Comment
Question by:esc_toe_account
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 125 total points
ID: 34160052
You might be able to simplify it to (untested mind you):
static public int ChangedCount(this ObjectStateManager manager)
    {
        int changedCount = 0;
        try
        {
            changedCount += manager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified).Count<ObjectStateEntry>();
        }
        catch {}
            
         return changedCount;
    }

Open in new window

0
 

Author Comment

by:esc_toe_account
ID: 34160127
This does simplify it. Plus it is now 3 times faster (if there is no dirt).

I guess one line of code is not bad but I would still like to hear whether there is a property in the manager that simply says dirty or not.
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 125 total points
ID: 34161222
If there is a property, I haven't found it yet.  All roads point to "ObjectContext.ObjectStateManager.GetObjectStateEntries".
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now