All, Say you have some code like below. I want to know If It didn’t run the db.savechanges(), Is it necessary to write some code to Cancel db.DeployPackages.Add(package) commonly?
HotDeployDbContext db = null;
using (db = new HotDeployDbContext())
{
DeployPackage package = new DeployPackage();
db.DeployPackages.Add(package);
//determine if save package to db based on some condition.
if (.....)
{
db.SaveChanges();
}
else
{
//how to cancel the db.DeployPackages.Add(package);
}
}
Edited
Sometimes we do lot of things in one Dbcontext, like add data to multiple table. delete some data or update data . but what if we do need to cancel these before run SaveChanges based on some business condition? How can I make this .Thanks.
When the code exits the using block, all data will be thrown away and not saved.
Microsoft’s official stance is that if you want to back out changes, you throw away the DbContext. There is no good way to remove objects.