How to dispose the context variable in c#?

Total Post:134

Points:940

C# 
C#
 2101  View(s)
Ratings:
Rate this:

I want to dispose the devDB context object in the below code.

private static void InitContainer()
{
     var devDB = new TestContext(constr); 
                 Container                
                    .RegisterInstance<TestContext>(devDB)
                    .RegisterInstance<IRepository<User>>(new Repository<User>(devDB))
}
private static void CleanUp()
{
    if (Container != null)
    {
        Container.Dispose();
    }
}

I tried a using statement, but the I receive the following error:

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

How can I dispose devDB correctly?

  1. Re: How to dispose the context variable in c#?

    Hi Tanuj,

    Use using statements:

    using(var devDB = new TestContext(constr))
    {
        // rest of your code
    }

    It will automatically Dispose your variable if it's Disposable.

      Modified On Apr-04-2018 01:36:06 AM

Answer

Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.