© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•10mo ago•
3 replies
jorgedotnet

[SOLVED] Integration Tests | .Net Core WebApi | Fails seeds data when overriding a service in a test

Context: I've coded a custom WebApplicationFactory to setup a web host configuring an in-memory database and seeding data for
User
User
table.
Expected Test: Ensure global handling exception when an exception is trown in GetById() userService method.
Issue : When I run it, it fails in
dbContext.SaveChanges();
dbContext.SaveChanges();
logging that it couldn't seed the data, I've debug it and saw that it only happens after overriding the service.

 Message: 
Microsoft.EntityFrameworkCore.DbUpdateException : An error occurred while saving the entity changes. See the inner exception for details.
---- Microsoft.Data.SqlClient.SqlException : Cannot insert explicit value for identity column in table 'Users' when IDENTITY_INSERT is set to OFF.

  Stack Trace: 
ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
BatchExecutor.Execute(IEnumerable1 commandBatches, IRelationalConnection connection)
RelationalDatabase.SaveChanges(IList1 entries)
StateManager.SaveChanges(IList1 entriesToSave)
StateManager.SaveChanges(StateManager stateManager, Boolean acceptAllChangesOnSuccess)
<>c.<SaveChanges>b__104_0(DbContext _, ValueTuple2 t)
SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded)
StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
DbContext.SaveChanges()
 Message: 
Microsoft.EntityFrameworkCore.DbUpdateException : An error occurred while saving the entity changes. See the inner exception for details.
---- Microsoft.Data.SqlClient.SqlException : Cannot insert explicit value for identity column in table 'Users' when IDENTITY_INSERT is set to OFF.

  Stack Trace: 
ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
BatchExecutor.Execute(IEnumerable1 commandBatches, IRelationalConnection connection)
RelationalDatabase.SaveChanges(IList1 entries)
StateManager.SaveChanges(IList1 entriesToSave)
StateManager.SaveChanges(StateManager stateManager, Boolean acceptAllChangesOnSuccess)
<>c.<SaveChanges>b__104_0(DbContext _, ValueTuple2 t)
SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded)
StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
DbContext.SaveChanges()
code1.png
code2.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

✅ Overriding IConfigurationManager.GetSection for integration tests in .NET 8 Minimal API
C#CC# / help
14mo ago
C# .Net Core WebAPI Cyclic Dependancy
C#CC# / help
2y ago
Testcontainers in integration tests vs BackgroundService
C#CC# / help
3y ago
Writing integration tests for .net core endpoints with authentication
C#CC# / help
3y ago