Maxim Polishchuk
Maxim Polishchuk

Reputation: 334

Don't dispose property after "using" statement

I have small question by entity framework 4.1.

There is interface of image DTO:

public interface IImage : IDtoBase
{
    string FullFilePath { get; set; }
    int Width { get; set; }
    int Heigth { get; set; }
    long ImageTypeId { get; set; }
    IImageType Type { get; set; }
}

There is code to configure context:

// where TContext : DbContext, new()
private TInterface Invoke(Func<TContext, TInterface> callback)
{
    using (var context = new TContext())
    {
        context.Configuration.AutoDetectChangesEnabled = true;
        context.Configuration.LazyLoadingEnabled = true;
        context.Configuration.ProxyCreationEnabled = true;
        context.Configuration.ValidateOnSaveEnabled = true;

        context.Database.Connection.Open();

        return callback.Invoke(context);
    }
}

There is code to get required DTO item:

public TInterface Get(long id)
{
    return Invoke(
        context =>
        {
            TDto dto = context.Set<TDto>().FirstOrDefault(x => (x.Id == id));
            return dto.Convert<TDto, TInterface>();
        }
    );
}

If I set context.Configuration.LazyLoadingEnabled = false, then "Type" property of image DTO is null (I think, that's ok).

If context.Configuration.LazyLoadingEnabled has "true" value, then "Type" property of image DTO has correct value inside of "using" statement, but this property is disposed after disposing of context - "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection."

I.e. image DTO exists/is not disposed, but its "Type" property already is disposed.

Could anybody provide any solution - don't dispose "Type" property (I would like to use "using" statement instead of "Dispose" pattern)?

Upvotes: 4

Views: 687

Answers (1)

Eranga
Eranga

Reputation: 32437

First of all you need to understand how lazy loading works. EF does this by creating a proxy which inherits from your entity class and it overrides the navigational property access behavior. When you first access the navigational property, if it was not loaded before the proxy class has the logic to load it through the context. Hence proxy instances keep a reference to the context.

When execution leaves your Invoke method, the context will be disposed. Therefor the proxy can not lazy load the navigational properties. There are couple of things you can do to solve this.

  • Eager load navigational properties

use the Include method to eager load

public TInterface Get(long id)
{
    return Invoke(
        context =>
        {
            TDto dto = context.Set<TDto>().Include(t => t.Type)
                  .FirstOrDefault(x => (x.Id == id));

            return dto.Convert<TDto, TInterface>();
        }
    );
}
  • Control the lifetime of the context Without disposing the context immediately inside the Invoke method you can use an existing context instance which would be disposed after you are done with the data access. This can be done with IoC/DI framework. For a web projects the lifetime is usually limited to a single request.

Upvotes: 4

Related Questions