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

  • Home
  • SEARCH
  • 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 4622310
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:49:31+00:00 2026-05-22T02:49:31+00:00

When doing: using (IUnitOfWork uow = UnitOfWork.Current) { LocationRepository rep = new LocationRepository(); Location

  • 0

When doing:

    using (IUnitOfWork uow = UnitOfWork.Current)
    {
        LocationRepository rep = new LocationRepository();
        Location loc = new Location()
        {
            CompanyId = m_User.UserCompany.CompanyId,
            Name = locationName
        };
        rep.Insert(loc);
        uow.Commit();
    }

I get the exception:

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

The reason I get this exception is that m_User.UserCompany is implemented like:

public _Company UserCompany
{
    get
    {
                using (IUnitOfWork uow = UnitOfWork.Current)
                {
                    CompanyRepository rep = new CompanyRepository();
                    m_Company = rep.Get(companyId.Value);
                }
        return m_Company;
    }
}

So actually inside the first using statement I create another using statement which dispose my context when done and I get the exception in the outer using statement.

I am using the entity framework the way described here.

What is the common solution for such case?

  • 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-22T02:49:32+00:00Added an answer on May 22, 2026 at 2:49 am

    I wonder why do you even bother with repositories if your User entity access unit of work directly? Your architecture looks very inconsistent. Create central place where unit of work is instanciated and disposed – in case of web application ideal place to call these methods is beginning and ending request. Access unit of work from repositories (or pass unit of work as parameter to their constructors) and handle all data retrieving and data persisting in repositories. Don’t make your entities dependent on unit of work – that makes your repositories redundant and you can fallback to active record pattern.

    This all leads to dependency injection with per HTTP request lifetime for unit of work and repositories.

    Complains like “it is lot of work” or “I have a lot of places to change” are not excuse. You have a problem and you have to solve it. Solution requires big amount of refactoring because your initial solution was wrong – I sad to say you did it wrong and now you must invest your time to fix it.

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

Sidebar

Related Questions

Instead of doing: using(MyDataContext db = new MyDataContext()) { //do something } I'd like
I'm doing using(var scope = new TransactionScope()) { //lots of calls to sql server
I'm having a problem with a POST I'm doing using the HttpWebRequest object from
For a bit of background to know what i am doing: using hit highlighter
I created a database for a site i'm doing using Django as the admin
We need to represent huge numbers in our application. We're doing this using integer
I am doing regression testing using NUnit, WatiN and VB.net. What i am doing
I'm doing some development using JS unde Adobe AIR. What I want to do
I'm doing background subtraction using opencv. The problem is the foreground object is not
I have a small project I am doing in Python using web.py. It's a

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.