diff --git a/src/CSESoftware.Repository.EntityFrameworkCore/CSESoftware.Repository.EntityFrameworkCore.csproj b/src/CSESoftware.Repository.EntityFrameworkCore/CSESoftware.Repository.EntityFrameworkCore.csproj index 5b8118c..722e5b5 100644 --- a/src/CSESoftware.Repository.EntityFrameworkCore/CSESoftware.Repository.EntityFrameworkCore.csproj +++ b/src/CSESoftware.Repository.EntityFrameworkCore/CSESoftware.Repository.EntityFrameworkCore.csproj @@ -6,7 +6,7 @@ CSESoftware.Repository.EntityFrameworkCore CSE Software, Inc. 2020 CSE Software, Inc. - 2.0.0 + 2.0.1 packageIcon.png The Entity Framework Core implementation of CSESoftware.Repository. diff --git a/src/CSESoftware.Repository.EntityFrameworkCore/Repository.cs b/src/CSESoftware.Repository.EntityFrameworkCore/Repository.cs index bf08b5a..30b4fa7 100644 --- a/src/CSESoftware.Repository.EntityFrameworkCore/Repository.cs +++ b/src/CSESoftware.Repository.EntityFrameworkCore/Repository.cs @@ -34,14 +34,17 @@ public virtual void Update(TEntity entity) where TEntity : class, IEntity { entity.UpdateSetup(); - Context.Set().Attach(entity); + if (Context.Entry(entity).State == EntityState.Detached) + Context.Set().Attach(entity); + Context.Entry(entity).State = EntityState.Modified; } public virtual void Update(List entities) where TEntity : class, IEntity { - Context.Set().AttachRange(entities); + Context.Set().AttachRange( + entities.Where(x => Context.Entry(x).State == EntityState.Detached)); foreach (var entity in entities) { @@ -62,9 +65,8 @@ public virtual void Delete(TEntity entity) { var dbSet = Context.Set(); if (Context.Entry(entity).State == EntityState.Detached) - { dbSet.Attach(entity); - } + dbSet.Remove(entity); } @@ -83,9 +85,16 @@ public virtual void Delete(Expression> filter) Context.Set().RemoveRange(Context.Set().Where(filter)); } - public virtual Task SaveAsync() + public virtual async Task SaveAsync() + { + await Context.SaveChangesAsync(); + DetachAllEntities(); + } + + private void DetachAllEntities() { - return Context.SaveChangesAsync(); + Context.ChangeTracker.Entries().ToList() + .ForEach(x => x.State = EntityState.Detached); } } -} \ No newline at end of file +}