Search code examples
unity-containerobject-lifetimeentity-framework-4.3

Entity Framework Context dealing stale data


I'm using Unity to inject a context and using the following lifetime manager...

public class HttpContextLifetimeManager<T> : LifetimeManager, IDisposable
{
    #region IDisposable Members

    public void Dispose()
    {
        RemoveValue();
    }

    #endregion

    public override object GetValue()
    {
        object value = HttpContext.Current.Items[typeof (T).AssemblyQualifiedName];

        return value;
    }

    public override void RemoveValue()
    {
        HttpContext.Current.Items.Remove(typeof (T).AssemblyQualifiedName);
    }

    public override void SetValue(object newValue)
    {
        HttpContext.Current.Items[typeof (T).AssemblyQualifiedName]
            = newValue;
    }
}
  • First request to page one: Shows values.
  • First web request to page two:Updates values.
  • Second web request to page one: Shows old values.
  • Second web request to page two: Shows new values.

I have to restart the VS development server to get page one to show the new values.

So how can a context a) live between page requests and b) be specific to a page?


Solution

  • This had nothing to do with EF. The generated UI was out of sync with the entity and the entity was throwing validation errors which weren't being reported in the UI. It would have helped if the scaffolding templates had generated a validation summary which didn't only show model level errors.