Skip to main content

280 posts tagged with "C#"

C# Programming Language

View All Tags

Fixed: "Sequence contains no elements" with EF Core FirstAsync

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A request for a category that doesn't exist answered 500, with InvalidOperationException: Sequence contains no elements. The database was fine, and so was the SQL: it ran and found nothing. FirstAsync treats "nothing" as an exception. For an ID a client sent, nothing is a normal answer, so the code should use FirstOrDefaultAsync and reply 404.

Fixed: "Cannot access a disposed context instance" in EF Core

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

An endpoint started some work in the background and answered 200 OK straight away. Half a second later the log said ObjectDisposedException: Cannot access a disposed context instance. The DbContext belongs to the request: when the request ended, ASP.NET Core disposed of it, and the background task was still holding it. Background work needs a context of its own.

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.