this works fine as is, but what I really want is the above in a transaction block so that the deletion only occurs if the whole transaction is successful. How can I do this? Thanks
using (var t = new TransactionScope)
{
//Delete policy
//
CreateContext();
_context.AttachTo("Policies", policy);
_context.ObjectStateManager.ChangeObjectState(policy, EntityState.Deleted);
_context.SaveChanges(SaveOptions.None);
//
//Delete case
//
//CreateContext();
Case oCase = helper.GetCaseByCaseID(caseID);
_context.AttachTo("Cases", oCase);
_context.ObjectStateManager.ChangeObjectState(oCase, EntityState.Deleted);
_context.SaveChanges(SaveOptions.None);
t.Complete();
_context.AcceptAllChanges();
}
However on the first _context.SaveChanges, it doesn't matter if I pass no parameters or if I pass any value of the 'SaveOptions' Enum. I always receive an erro the underlying provider failed to open.
Not sure I understand. Since In my original code snippet that shows in the transaction. I call CreateContext and that sets it to
new So I'm already doing it.
billcrawley
Member
341 Points
286 Posts
EF context and Transaction scope.
Jan 08, 2013 11:44 AM|LINK
Hi all,
I use a single context in the following way.
//Delete policy // CreateContext(); _context.AttachTo("Policies", policy); _context.ObjectStateManager.ChangeObjectState(policy, EntityState.Deleted); _context.SaveChanges(SaveOptions.None); _context.Dispose(); // //Delete case // CreateContext(); Case oCase = helper.GetCaseByCaseID(caseID); _context.AttachTo("Cases", oCase); _context.ObjectStateManager.ChangeObjectState(oCase, EntityState.Deleted); _context.SaveChanges(SaveOptions.None); _context.Dispose();this works fine as is, but what I really want is the above in a transaction block so that the deletion only occurs if the whole transaction is successful. How can I do this? Thanks
HeartattacK
All-Star
55288 Points
5920 Posts
Moderator
MVP
Re: EF context and Transaction scope.
Jan 08, 2013 12:39 PM|LINK
You should be able to wrap all of that in:
using(var t = new TransactionScope()){
try{
//do operations
t.Complete();
}
finally{
//consider disposing connections / contexts here.
}
}
blog: www.heartysoft.com
twitter: @ashic
billcrawley
Member
341 Points
286 Posts
Re: EF context and Transaction scope.
Jan 09, 2013 11:22 AM|LINK
using (var t = new TransactionScope) { //Delete policy // CreateContext(); _context.AttachTo("Policies", policy); _context.ObjectStateManager.ChangeObjectState(policy, EntityState.Deleted); _context.SaveChanges(SaveOptions.None); // //Delete case // //CreateContext(); Case oCase = helper.GetCaseByCaseID(caseID); _context.AttachTo("Cases", oCase); _context.ObjectStateManager.ChangeObjectState(oCase, EntityState.Deleted); _context.SaveChanges(SaveOptions.None); t.Complete(); _context.AcceptAllChanges(); }However on the first _context.SaveChanges, it doesn't matter if I pass no parameters or if I pass any value of the 'SaveOptions' Enum. I always receive an erro the underlying provider failed to open.
HeartattacK
All-Star
55288 Points
5920 Posts
Moderator
MVP
Re: EF context and Transaction scope.
Jan 09, 2013 11:23 AM|LINK
You need to put t.Complete after AcceptChanges.
blog: www.heartysoft.com
twitter: @ashic
billcrawley
Member
341 Points
286 Posts
Re: EF context and Transaction scope.
Jan 09, 2013 12:00 PM|LINK
Ok. but I'm not getting that far.
HeartattacK
All-Star
55288 Points
5920 Posts
Moderator
MVP
Re: EF context and Transaction scope.
Jan 09, 2013 12:08 PM|LINK
What's CreateContext() doing? Are the correct connectionstrings present in the config file?
blog: www.heartysoft.com
twitter: @ashic
billcrawley
Member
341 Points
286 Posts
Re: EF context and Transaction scope.
Jan 09, 2013 01:43 PM|LINK
THis is createcontext
private ObjectContext _context; private void CreateContext() { _context = new ObjectContext("name=Default_Connection"); _context.DefaultContainerName = "Default_Connection"; if (!_context.DatabaseExists()) { _context = new ObjectContext("name=Backup_Connection"); _context.DefaultContainerName = "Default_Connection"; } _context.ContextOptions.LazyLoadingEnabled = true; }If I keep the code outside the transaction, then each update ok.
HeartattacK
All-Star
55288 Points
5920 Posts
Moderator
MVP
Re: EF context and Transaction scope.
Jan 09, 2013 02:08 PM|LINK
Try new ObjectContext("Default_Connection");
That said, have you considered not directly using ObjectContext()?
blog: www.heartysoft.com
twitter: @ashic
billcrawley
Member
341 Points
286 Posts
Re: EF context and Transaction scope.
Jan 09, 2013 02:44 PM|LINK
Not sure I understand. Since In my original code snippet that shows in the transaction. I call CreateContext and that sets it to new So I'm already doing it.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: EF context and Transaction scope.
Jan 10, 2013 01:25 AM|LINK
Hi,
You can try to combine the two actions together for rolling back:
using (var t = new TransactionScope) { EFContext _context = new EFContext(); _context.AttachTo("Policies", policy); _context.ObjectStateManager.ChangeObjectState(policy, EntityState.Deleted); Case oCase = helper.GetCaseByCaseID(caseID); _context.AttachTo("Cases", oCase); _context.ObjectStateManager.ChangeObjectState(oCase, EntityState.Deleted); _context.SaveChanges(SaveOptions.None); t.Complete(); _context.AcceptAllChanges(); }