Solved

Determining if ObjectStateManager has dirty records

Posted on 2010-11-17
3
1,350 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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…

911 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

25 Experts available now in Live!

Get 1:1 Help Now