Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disposing of object context in entity framework 4

I have an entity class which is auto generated from my database model. This class inherits the ObjectContext which inturn inherits IDisposable.

I have created a repository that has various methods which use a single instance of the entity object to interact with the database.

Auto generated class

public partial class DevEntities : ObjectContext
{
    public const string ConnectionString = "name=DevEntities";
    public const string ContainerName = "DevEntities";

Repository Class

DevEntities db = new DevEntities();

        public Customer GetCustomerByID(int id)
    {
        var customers = db.Customers.FirstOrDefault(c => c.CustomerId == id);

        return customers;
    }

    public Customer GetCustomerByPasswordUsername(string email, string password)
    {
        var customers = db.Customers.FirstOrDefault(c => c.Email == email && c.Password == password);

        return customers;
    }

From this you can see that I make multiple references to the db instance. My question is, am I better to instantiate a new DevEntity within each method, thus being able to implement the using statement, and so ensure correct disposal, or is my current implementation ok?

like image 977
hoakey Avatar asked Jan 02 '11 15:01

hoakey


People also ask

How to properly dispose of DbContext?

Although the DbContext implements IDisposable , you shouldn't manually dispose it, nor should you wrap it in a using statement. DbContext manages its own lifetime; when your data access request is completed, DbContext will automatically close the database connection for you.

Should I dispose DbContext ef core?

For ASP.NET core DbContext instance is created for each request and disposed when the request ends: docs.microsoft.com/en-us/ef/core/dbcontext-configuration This does NOT happen for other types of applications automatically. So for a UWP application, it is necessary to dispose DbContext at the end of the unit-of-work.

How do you know if DbContext is disposed?

Having the IsDisposed method defined, you can check if the context is disposed before calling SaveChanges (or any other method that are dangerous to be invoked on a disposed context): if ((dbContext != null) && (! dbContext.

What is object context in Entity Framework?

The ObjectContext class is the primary class for interacting with data as objects that are instances of entity types that are defined in a conceptual model. An instance of the ObjectContext class encapsulates the following: A connection to the database, in the form of an EntityConnection object.


1 Answers

I would implement IDisposable on the Repository class as well, so it can dispose the ObjectContext. If you return a different ObjectContext each time, you can run into problems when doing queries between those objects, as those are attached to different ObjectContexts, which will result in an exception.

Definition:

public class Repository : IDisposable
{
    DevEntities db = new DevEntities();

    public Customer GetCustomerByID(int id)
    {
        var customers = db.Customers.FirstOrDefault(c => c.CustomerId == id);

        return customers;
    }

    public Customer GetCustomerByPasswordUsername(string email, string password)
    {
        var customers = db.Customers.FirstOrDefault(c => c.Email == email && c.Password == password);

        return customers;
    }

    public void Dispose()
    {
        db.Dispose();
    }
}

Usage:

using(Repository r = new Repository())
{
  //do stuff with your repository
}

Doing this, your repository takes care of disposing the ObjectContext after you used it.

like image 159
Femaref Avatar answered Oct 18 '22 13:10

Femaref