Skip to content

Commit 71b7988

Browse files
authored
docs: change tracker clear (#50)
Fix #48
1 parent 1075a46 commit 71b7988

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

.github/USAGE.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,24 @@ protected override void ConfigureDbContext(DbContextOptionsBuilder builder)
193193

194194
This beahvior WILL drop your database after each test !
195195

196+
### Cleaning the ChangeTracker
197+
198+
When running normally, dotnet web applications will use a different scoped
199+
instance of the DbContext for each HTTP request. The test library uses a
200+
singleton instance, otherwise it would not be possible to access the same
201+
instance from within the test context. As a consequence, any call to the
202+
DbContext during the arrange phase (including calls to the HttpClient) might
203+
clogger the Change Tracker with existing entities. In this situation the
204+
DbContext's ChangeTracker might not be empty when the system under test is
205+
called. This may in turn cause attaching entities to fail, whereas it would
206+
have worked in a real request.
207+
208+
The most common pattern for this is when arranging some entities in the
209+
database then calling an update entity operation.
210+
211+
In such case, the test writer should call `Database.ChangeTracker.Clear()`
212+
at the end of the arrange phase.
213+
196214
## OpenTelemetry integration
197215

198216
The library provides specific support for otel. You can achieve this

0 commit comments

Comments
 (0)