Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8082579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:09:40+00:00 2026-06-05T17:09:40+00:00

I have small question by entity framework 4.1. There is interface of image DTO:

  • 0

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)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:09:42+00:00Added an answer on June 5, 2026 at 5:09 pm

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small question about the Core Bluetooth framework. I know Core Bluetooth
There is a similar question here on SO, but I have a fairly small
I'm having a small problem with ASP.NET MVC and Entity Framework 4. I have
I have a fairly simple question. There is a named HTML entity in most
I have a small question; In PHP I have used curl to get data
Sorry for my English. I have a a small question: There is a variable
I have small question. When I started to programm my application, I grouped types
hello I have a small question in objective-C memory management. I know that if
I'm not much into COM interfaces, so i have a small question, say I
I have a small sql question for you. I have two queries: SELECT tbl_CostPrevisions.Month,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.