Problem with dbEntityEntry.State - always unmodified. How to resolve?

Feb 6, 2014 at 9:41 PM
Edited Feb 6, 2014 at 9:46 PM
Great video. Clarified a few things for me in terms of file placement etc.

I'm having an issue with the dbEnetityEntry.State in DataContext.cs.

In DataContext.CS (line 63)
    private void SyncObjectsStatePreCommit()
    {
        foreach (var dbEntityEntry in ChangeTracker.Entries())
            dbEntityEntry.State = StateHelper.ConvertState(((IObjectState)dbEntityEntry.Entity).ObjectState);
    }
Going into the loop, dbEntityEntry.State = modified. However in executing the StateHelper.ConvertState() the sate is "Unchanged".

So, dbEntityEntry.State = "Modified", but StateHelper.ConvertState(((IObjectState)dbEntityEntry.Entity).ObjectState) = "Unmodified"

For now, I've just done the following (mainly so I can add a breakpoint:
    private void SyncObjectsStatePreCommit()
    {
        foreach (var dbEntityEntry in ChangeTracker.Entries())
        {
            //dbEntityEntry.State = StateHelper.ConvertState(((IObjectState)dbEntityEntry.Entity).ObjectState);
            dbEntityEntry.State = dbEntityEntry.State;
        }
    }

    private void SyncObjectsStatePostCommit()
    {
        foreach (var dbEntityEntry in ChangeTracker.Entries())
        {
            //((IObjectState)dbEntityEntry.Entity).ObjectState = StateHelper.ConvertState(dbEntityEntry.State);
            dbEntityEntry.State = dbEntityEntry.State;
        }
    }
Why isn't it picking up the different state?
Coordinator
Feb 10, 2014 at 2:43 AM
Hi elahix, could you please post the actual code of your implementation?
Mar 13, 2014 at 11:40 AM
Just looking at this again...

When performing the following:
        try
        {
            // perform mapping from DTO to model object - do not use AutoMapper as it is not thread-safe
            var entity = new ContentType
            {
                ContentTypeId = key,
                UserId = 102, // mock
                Description = contentTypeDTO.Description
            };

            _uow.Repository<ContentType>().Update(entity);
            _uow.Save();
            return Updated(contentTypeDTO);
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!ContentTypeExists(key))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
In SyncObjectsStatePreCommit,
            //dbEntityEntry.State = StateHelper.ConvertState(((IObjectState)dbEntityEntry.Entity).ObjectState);
            dbEntityEntry.State = dbEntityEntry.State;
Both dbEntityEntry.state and StateHelper.ConvertState(((IObjectState)dbEntityEntry.Entity).ObjectState); show as modified. Should I uncomment the original code?
Coordinator
Mar 14, 2014 at 8:34 AM
Make sure you are setting IObjectState for every entity in the graph and that you are using the same IDataContext e.g. setting DI & IoC up correctly e.g. every time you are injected with IDataContext async it should be the same instance.
Marked as answer by lelong37 on 3/31/2014 at 10:25 PM