Index: Application/Ringtoets/src/Application.Ringtoets.Storage/IStorableExtensions.cs =================================================================== diff -u -rb5c11c4e073b7e10365ddbe62f83418ebaba14c4 -rbb04ea61be6e4ce94cb1453c4578b0e9256168c4 --- Application/Ringtoets/src/Application.Ringtoets.Storage/IStorableExtensions.cs (.../IStorableExtensions.cs) (revision b5c11c4e073b7e10365ddbe62f83418ebaba14c4) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/IStorableExtensions.cs (.../IStorableExtensions.cs) (revision bb04ea61be6e4ce94cb1453c4578b0e9256168c4) @@ -21,7 +21,8 @@ using System; using System.Data.Entity; -using System.Linq; + +using Application.Ringtoets.Storage.DbContext; using Application.Ringtoets.Storage.Exceptions; using Application.Ringtoets.Storage.Properties; using Core.Common.Base.Storage; @@ -48,29 +49,38 @@ /// /// The corresponding with the Entity. /// The Entity set to obtain the existing entity from. - /// The function to obtain the id field of the Entity. + /// /// The stored as an Entity of type . /// Thrown when either: /// /// the Entity of type couldn't be found in the /// more than one Entity of type was found in the /// has not yet been saved in the database /// - public static T GetCorrespondingEntity(this IStorable storable, DbSet entitySet, Func getId) where T : class + public static T GetCorrespondingEntity(this IStorable storable, DbSet entitySet, IRingtoetsEntities context) where T : class { if (storable.IsNew()) { throw new EntityNotFoundException(string.Format(Resources.Error_Entity_Not_Found_0_1, typeof(T).Name, storable.StorageId)); } try { - Func expression = sle => getId(sle) == storable.StorageId; - return entitySet.Single(expression); + context.AutoDetectChangesEnabled = false; + T entity = entitySet.Find(storable.StorageId); + if (entity == null) + { + throw new EntityNotFoundException(string.Format(Resources.Error_Entity_Not_Found_0_1, typeof(T).Name, storable.StorageId)); + } + return entity; } catch (InvalidOperationException exception) { throw new EntityNotFoundException(string.Format(Resources.Error_Entity_Not_Found_0_1, typeof(T).Name, storable.StorageId), exception); } + finally + { + context.AutoDetectChangesEnabled = true; + } } } } \ No newline at end of file