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 206731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:40:20+00:00 2026-05-11T17:40:20+00:00

EDIT: Duplicate of Should Entity Framework Context be Put into Using Statement? I’ve been

  • 0

EDIT: Duplicate of Should Entity Framework Context be Put into Using Statement?

I’ve been tossing around this idea for some time wondering what bad could happen by not properly disposing my object context and allowing it to die with the GC. Normally, I would shun this, but there is a valid reason to do it.

We are using partial classes. In those partial classes we expose properties that access FK objects. For example, let’s say I have a Customer class with a CustomerType FK object. In the class, I would expose a CustomerTypeName property that does this:

public string CustomerTypeName {
  get {
     if (CustomerType == null) {
         CustomerTypeReference.Load() 
     }

     return CustomerType.CustomerTypeName;
  }
}

This works out very handy if the original query did not do a .Include(“CustomerType”).

However, if I dispose the context, the above property no longer works. So… I guess this leads to a couple of questions:

1) If I never explicitly dispose of the context, what negatives will I see, if any?
2) Is there any other way to accomplish lazy loading in the above scenario and still dispose of the context?

  • 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-05-11T17:40:20+00:00Added an answer on May 11, 2026 at 5:40 pm

    Well ObjectContext’s that are left around indefinitely are fine, so long as you don’t keep loading / adding lots of new objects.

    Every object that is loaded or added will always be tracked by the ObjectContext until it is disposed, so if you never dispose, and you keep tracking more objects it will just get bigger and bigger.

    One option you could look at doing is using some utility method to either access some well known context or create a temporary context.

    The key to this is using the EntityReference.EntityKey and making sure both entities are detached.

    i.e.

    this.CustomerType = Utility.GetDetachedObjectByKey<Customer>(
           this.CustomerTypeReference.EntityKey);
    

    The basic implementation of GetDetachedObjectByKey is something like this:

    public static T GetDetachedObjectByKey<T>(EntityKey key) 
         where T: EntityObject
    {
        using (MyContext ctx = new MyContext())
        {
            T t = ctx.GetObjectByKey(key) as T;
            ctx.Detach(t);
            return t;
        }
    }
    

    This will only work if the original object target is detached too. You could experiment with where the Context used by this method comes from.

    Hope this helps

    Alex

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Assuming this is a standard Apache log, and assuming you… May 11, 2026 at 11:49 pm
  • Editorial Team
    Editorial Team added an answer From Wikipedia: In software, a stack overflow occurs when too… May 11, 2026 at 11:49 pm
  • Editorial Team
    Editorial Team added an answer Option 1: c:...>dumpbin /summary whatever.exe Look for a ".textbss" section.… May 11, 2026 at 11:49 pm

Related Questions

EDIT: Duplicate of Should Entity Framework Context be Put into Using Statement? I've been
Each product in my database can have at least three and sometimes four or
Can Interfaces be a substitute for Multiple Inheritance in C#? EDIT: This is a
I would like to be able to display a DateTimePicker that has a default
I'm building a web application on .Net and it will run on 64 bit

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.