Entity Framework – Transaction Rollback – Remove the Attached Entities from next SaveChange outside Transaction Scope
using (var t = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted })) { try { … … t.Complete(); } catch (Exception ex) { … … t.Dispose(); } } this.DetachAllEntities(); |
protected void DetachAllEntities() { var changedEntriesCopy = this.dao.Context.ChangeTracker.Entries() .Where( e => e.State == System.Data.Entity.EntityState.Added || e.State == System.Data.Entity.EntityState.Modified || e.State == System.Data.Entity.EntityState.Deleted ) .ToList(); foreach (var entity in changedEntriesCopy) { this.baseDao3.Context.Entry(entity.Entity).State = System.Data.Entity.EntityState.Detached; } } |