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?
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.
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.
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.
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.
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 ObjectContext
s, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With