Skip to main content

Fixed: "A second operation was started on this context instance before a previous operation completed" in EF Core

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

To save time, I started three EF Core queries together and waited for them with Task.WhenAll. The API answered 500: A second operation was started on this context instance before a previous operation completed. Then I ran the same code again, and it worked. A DbContext can only do one thing at a time, and when you break that rule, whether you notice depends on timing.

Fixed: "The instance of entity type cannot be tracked because another instance with the same key value is already being tracked" in EF Core

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

I loaded a row to check it existed, built a new object with the same ID and the changed values, and called db.Categories.Update. EF Core refused: The instance of entity type 'Category' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. A context allows one object per row. The simple fix is to change the object you loaded, with no Update call at all.

Fixed: "JSON deserialization for type was missing required properties" in an ASP.NET Core Minimal API

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

A POST with an empty JSON body answered 400 Bad Request with no explanation, and the log said JSON deserialization for type 'Dewiride.Inventory.Data.Entities.Category' was missing required properties including: 'name'. The C# required keyword on my entity was doing its job, too early and too quietly. A request record with validation attributes says the same thing in a way a client can read.

Fixed: "A possible object cycle was detected" When Returning EF Core Entities from a Web API

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

An endpoint that returned EF Core entities answered 500, with JsonException: A possible object cycle was detected. The path in the message gave it away: $.Products.Category.Products.Category.Products…. A category has products, and each product points back to its category, so the JSON would never end. The fix isn't a JSON setting: it's not returning entities in the first place.

Fixed: DbUpdateConcurrencyException "The database operation was expected to affect 1 row(s), but actually affected 0 row(s)" in EF Core

· 9 min read
Jagdish Kumawat
Founder @ Dewiride

The database operation was expected to affect 1 row(s), but actually affected 0 row(s); data may have been modified or deleted since entities were loaded. EF Core expected its UPDATE to change one row, and MySQL said none. I got this message three different ways while building an API, and each needs a different answer. Telling them apart takes one look at the SQL.

Fixed: "InvalidCastException: Unable to cast object of type 'System.Attribute[]' to type 'IEnumerable`1[ValidationAttribute]'" with AsParameters in /openapi/v1.json

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

The API itself worked, but /openapi/v1.json started answering 500: InvalidCastException: Unable to cast object of type 'System.Attribute[]' to type 'System.Collections.Generic.IEnumerable1[System.ComponentModel.DataAnnotations.ValidationAttribute]'.The cause was a record used with[AsParameters], with validation attributes on its constructor parameters. It's a bug in ASP.NET Core 10, and adding property:` to each attribute avoids it.

Fixed: "BadHttpRequestException: Failed to read parameter from the request body as JSON" Returns 500 Instead of 400 in a Minimal API

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

A request with broken JSON got a 500 Internal Server Error from my minimal API, and the log said BadHttpRequestException: Failed to read parameter "CategoryRequest request" from the request body as JSON. The client's request was wrong, so it should have been a 400. It turned out to depend on the environment: in Development the bad request throws, and UseExceptionHandler turns anything thrown into a 500 unless you tell it otherwise.

Fixed: "Unable to retrieve project metadata. Ensure it's an SDK-style project." in dotnet ef

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

In a freshly cloned .NET project, dotnet ef stopped with Unable to retrieve project metadata. Ensure it's an SDK-style project. It's an SDK-style project, and the message sent me looking in the wrong place. The real clue is the line above it, NETSDK1004: Assets file … not found: nobody had run a restore in this copy yet.

Fixed: "The model for context has pending changes. Add a new migration before updating the database." in EF Core

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

dotnet ef database update printed The model for context 'InventoryDbContext' has pending changes., then No migrations were applied, then Done., and exited with code 0. My new column wasn't in MySQL. That message has two possible causes, and with this provider it won't stop a script, so it's easy to miss.