[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Determining if ObjectStateManager has dirty records

Posted on 2010-11-17
3
Medium Priority
?
1,389 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 500 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 500 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

834 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