[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Determining if ObjectStateManager has dirty records

Posted on 2010-11-17
3
Medium Priority
?
1,377 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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 will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

649 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