I'm working on a c# project which uses Entity Framework 6 for MySql. for Adding objects to the dbContext I use the following approach:
public void Add(User item)
{
using (var ctx = new DbContext())
{
ctx.Users.Add(item);
ctx.SaveChanges();
}
}
Now I need to know if it is ok to have one DbContext object for instance:
private DbContext _ctx = new DbContext();
and change the Add method to
public void Add(User item)
{
_ctx.Users.Add(item);
_ctx.SaveChanges();
}
And then to have another method to dispose the DbContext object and call it when the application is quitting.
Is it a good approach? what are the cons and pros of this approach?
thanks in advanced.