I have a 2 DbContext'
s that I call both of them in one form as you can see here:
dbcontext1.add(object);
dbcontext1.save();
dbcontext2.add(object);
dbcontext2.save();
So I have a question: if some problems happen and my record saved by DbContext1
but not in DbContext2
, I need to rollback my transaction.
I searched and I found two methods: using BeginTransaction
and TransactionScope
. I am confused about which one I should use and what the differences between them are?
I found something like this but i don;'t know how can i roll back this :
using (TransactionScope scope = new TransactionScope())
{
using (EFEntities1 dc = new EFEntities1())
{
dc.USERS.Add(user);
dc.SaveChanges();
}
using (EFEntities2 dc = new EFEntities2())
{
dc.USERS.Add(user);
dc.SaveChanges();
}
scope.complete();
}
Best regards