Skip to content

Commit bff57f9

Browse files
authored
Populate DbUpdateException.Entries for non-concurrency exceptions
Fixes #7829
1 parent 0933ff5 commit bff57f9

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/EFCore.Relational/Update/ReaderModificationCommandBatch.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,10 @@ public override void Execute(IRelationalConnection connection)
254254
}
255255
catch (Exception ex)
256256
{
257-
throw new DbUpdateException(RelationalStrings.UpdateStoreException, ex);
257+
throw new DbUpdateException(
258+
RelationalStrings.UpdateStoreException,
259+
ex,
260+
ModificationCommands.SelectMany(c => c.Entries).ToList());
258261
}
259262
}
260263

@@ -291,7 +294,10 @@ public override async Task ExecuteAsync(
291294
}
292295
catch (Exception ex)
293296
{
294-
throw new DbUpdateException(RelationalStrings.UpdateStoreException, ex);
297+
throw new DbUpdateException(
298+
RelationalStrings.UpdateStoreException,
299+
ex,
300+
ModificationCommands.SelectMany(c => c.Entries).ToList());
295301
}
296302
}
297303

test/EFCore.SqlServer.FunctionalTests/SqlServerValueGenerationScenariosTest.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,8 @@ public void Insert_with_ValueGeneratedOnAdd_GUID_nonkey_property_throws()
708708
Assert.Equal(default, blog.NotId);
709709

710710
// No value set on a required column
711-
Assert.Throws<DbUpdateException>(() => context.SaveChanges());
711+
var updateException = Assert.Throws<DbUpdateException>(() => context.SaveChanges());
712+
Assert.Single(updateException.Entries);
712713
}
713714

714715
public class BlogContextClientGuidNonKey : ContextBase
@@ -794,8 +795,10 @@ public void Insert_with_explicit_non_default_keys_by_default()
794795
// SqlException : Cannot insert explicit value for identity column in table
795796
// 'Blog' when IDENTITY_INSERT is set to OFF.
796797
context.Database.CreateExecutionStrategy().Execute(
797-
context, c =>
798-
Assert.Throws<DbUpdateException>(() => c.SaveChanges()));
798+
context, c => {
799+
var updateException = Assert.Throws<DbUpdateException>(() => c.SaveChanges());
800+
Assert.Single(updateException.Entries);
801+
});
799802
}
800803

801804
[ConditionalFact]
@@ -812,7 +815,8 @@ public void Insert_with_explicit_default_keys()
812815
// inner exception for details.
813816
// SqlException : Cannot insert explicit value for identity column in table
814817
// 'Blog' when IDENTITY_INSERT is set to OFF.
815-
Assert.Throws<DbUpdateException>(() => context.SaveChanges());
818+
var updateException = Assert.Throws<DbUpdateException>(() => context.SaveChanges());
819+
Assert.Single(updateException.Entries);
816820
}
817821

818822
public class BlogContext : ContextBase

test/EFCore.SqlServer.FunctionalTests/StoreGeneratedSqlServerTest.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ public virtual void Exception_in_SaveChanges_causes_store_values_to_be_reverted(
6666
new object[] { context.Entry(entity).Property(p => p.Id).CurrentValue }).Entity);
6767
}
6868

69-
Assert.Throws<DbUpdateException>(() => context.SaveChanges());
69+
// DbUpdateException : An error occurred while updating the entries. See the
70+
// inner exception for details.
71+
// SqlException : Cannot insert explicit value for identity column in table
72+
// 'Blog' when IDENTITY_INSERT is set to OFF.
73+
var updateException = Assert.Throws<DbUpdateException>(() => context.SaveChanges());
74+
Assert.Single(updateException.Entries);
7075

7176
foreach (var entity in entities.Take(100))
7277
{

0 commit comments

Comments
 (0)