From 9f6b43976fa9fed9b4d06c2df61734e736828164 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Sandstr=C3=B6m?= Date: Mon, 13 May 2024 16:07:14 +0200 Subject: [PATCH 1/4] Add new methods to DbDomainService: AttachAsAdded, -Modified and -Removed --- .../Framework/DbDomainService.cs | 62 ++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/src/OpenRiaServices.EntityFramework/Framework/DbDomainService.cs b/src/OpenRiaServices.EntityFramework/Framework/DbDomainService.cs index 3fcab08e9..d51a40eb3 100644 --- a/src/OpenRiaServices.EntityFramework/Framework/DbDomainService.cs +++ b/src/OpenRiaServices.EntityFramework/Framework/DbDomainService.cs @@ -267,7 +267,7 @@ private static async Task SetChangeSetConflictsAsync(Dictionary + /// Insert the entity into the specified db set + /// + /// Entity type + /// The db set + /// The modified entity + protected void AttachAsAdded(DbSet dbSet, TEntity entity) + where TEntity : class + { + EntityEntry entityEntry = DbContext.Entry(entity); + + if (entityEntry.State != EntityState.Detached) + { + entityEntry.State = EntityState.Added; + } + else + { + dbSet.Add(entity); + } + } + + /// + /// Mark the object for removal from the specified db set + /// + /// Entity type + /// The db set + /// The entity to remove + protected void AttachAsDeleted(DbSet dbSet, TEntity entity) + where TEntity : class + { + EntityEntry entityEntry = DbContext.Entry(entity); + + if (entityEntry.State != EntityState.Deleted) + { + entityEntry.State = EntityState.Deleted; + } + else + { + dbSet.Attach(entity); + dbSet.Remove(entity); + } + } + + /// + /// Mark the entity as modified in the specified db set + /// + /// Entity type + /// The db set + /// The modified entity + protected void AttachAsModified(DbSet dbSet, TEntity entity) + where TEntity : class + { + var original = ChangeSet.GetOriginal(entity); + + if (original != null) + dbSet.AttachAsModified(entity, original, DbContext); + else + dbSet.AttachAsModified(entity, DbContext); + } } } From 861dafc15e25f68e228758d57b26b5fc755e9154 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Sandstr=C3=B6m?= Date: Wed, 15 May 2024 07:54:56 +0200 Subject: [PATCH 2/4] Make dbSet parameter optional --- .../Framework/DbDomainService.cs | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/OpenRiaServices.EntityFramework/Framework/DbDomainService.cs b/src/OpenRiaServices.EntityFramework/Framework/DbDomainService.cs index d51a40eb3..d818b8a59 100644 --- a/src/OpenRiaServices.EntityFramework/Framework/DbDomainService.cs +++ b/src/OpenRiaServices.EntityFramework/Framework/DbDomainService.cs @@ -306,12 +306,12 @@ private static async Task SetChangeSetConflictsAsync(Dictionary - /// Insert the entity into the specified db set + /// Mark the entity as added /// /// Entity type - /// The db set /// The modified entity - protected void AttachAsAdded(DbSet dbSet, TEntity entity) + /// The db set + protected void AttachAsAdded(TEntity entity, DbSet dbSet = null) where TEntity : class { EntityEntry entityEntry = DbContext.Entry(entity); @@ -322,17 +322,18 @@ protected void AttachAsAdded(DbSet dbSet, TEntity entity) } else { + dbSet ??= DbContext.Set(); dbSet.Add(entity); } } /// - /// Mark the object for removal from the specified db set + /// Mark the object for removal /// /// Entity type - /// The db set /// The entity to remove - protected void AttachAsDeleted(DbSet dbSet, TEntity entity) + /// The db set + protected void AttachAsDeleted(TEntity entity, DbSet dbSet = null) where TEntity : class { EntityEntry entityEntry = DbContext.Entry(entity); @@ -343,20 +344,22 @@ protected void AttachAsDeleted(DbSet dbSet, TEntity entity) } else { + dbSet ??= DbContext.Set(); dbSet.Attach(entity); dbSet.Remove(entity); } } /// - /// Mark the entity as modified in the specified db set + /// Mark the entity as modified /// /// Entity type - /// The db set /// The modified entity - protected void AttachAsModified(DbSet dbSet, TEntity entity) + /// The db set + protected void AttachAsModified(TEntity entity, DbSet dbSet = null) where TEntity : class { + dbSet ??= DbContext.Set(); var original = ChangeSet.GetOriginal(entity); if (original != null) From 5664d1befb1621e2911a234370b7f6b6aea31ce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Sandstr=C3=B6m?= Date: Tue, 21 May 2024 15:26:12 +0200 Subject: [PATCH 3/4] Only use AttachAsModified and remove AsDeleted and AsRemoved. Remove dbSet as parameter --- .../Framework/DbDomainService.cs | 50 +------------------ 1 file changed, 2 insertions(+), 48 deletions(-) diff --git a/src/OpenRiaServices.EntityFramework/Framework/DbDomainService.cs b/src/OpenRiaServices.EntityFramework/Framework/DbDomainService.cs index d818b8a59..77669a460 100644 --- a/src/OpenRiaServices.EntityFramework/Framework/DbDomainService.cs +++ b/src/OpenRiaServices.EntityFramework/Framework/DbDomainService.cs @@ -305,61 +305,15 @@ private static async Task SetChangeSetConflictsAsync(Dictionary - /// Mark the entity as added - /// - /// Entity type - /// The modified entity - /// The db set - protected void AttachAsAdded(TEntity entity, DbSet dbSet = null) - where TEntity : class - { - EntityEntry entityEntry = DbContext.Entry(entity); - - if (entityEntry.State != EntityState.Detached) - { - entityEntry.State = EntityState.Added; - } - else - { - dbSet ??= DbContext.Set(); - dbSet.Add(entity); - } - } - - /// - /// Mark the object for removal - /// - /// Entity type - /// The entity to remove - /// The db set - protected void AttachAsDeleted(TEntity entity, DbSet dbSet = null) - where TEntity : class - { - EntityEntry entityEntry = DbContext.Entry(entity); - - if (entityEntry.State != EntityState.Deleted) - { - entityEntry.State = EntityState.Deleted; - } - else - { - dbSet ??= DbContext.Set(); - dbSet.Attach(entity); - dbSet.Remove(entity); - } - } - /// /// Mark the entity as modified /// /// Entity type /// The modified entity - /// The db set - protected void AttachAsModified(TEntity entity, DbSet dbSet = null) + protected void AttachAsModified(TEntity entity) where TEntity : class { - dbSet ??= DbContext.Set(); + var dbSet = DbContext.Set(); var original = ChangeSet.GetOriginal(entity); if (original != null) From c0cde52cf6f5d50f5ec829c58a407f660b53b5b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Sandstr=C3=B6m?= Date: Mon, 3 Jun 2024 14:06:19 +0200 Subject: [PATCH 4/4] use AttachAsModified in an Update method --- .../AdventureWorks/EFCore_Catalog.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Test/Desktop/OpenRiaServices.Common.DomainServices.Test/AdventureWorks/EFCore_Catalog.cs b/src/Test/Desktop/OpenRiaServices.Common.DomainServices.Test/AdventureWorks/EFCore_Catalog.cs index 7601af55e..99e5cc656 100644 --- a/src/Test/Desktop/OpenRiaServices.Common.DomainServices.Test/AdventureWorks/EFCore_Catalog.cs +++ b/src/Test/Desktop/OpenRiaServices.Common.DomainServices.Test/AdventureWorks/EFCore_Catalog.cs @@ -59,7 +59,7 @@ public void AddProduct(Product product) [Update] public void UpdateProduct(Product current) { - this.DbContext.Products.AttachAsModified(current, this.ChangeSet.GetOriginal(current), this.DbContext); + AttachAsModified(current); } [Delete]