Link to home
Start Free TrialLog in
Avatar of esc_toe_account
esc_toe_accountFlag for United States of America

asked on

Determining if ObjectStateManager has dirty records

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

ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of esc_toe_account

ASKER

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial