Skip to main content

272 posts tagged with "Issues"

Common Issues and Troubleshooting

View All Tags

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.

Fixed: "The migration 'AddProductVersion' was not found." with dotnet ef --no-build

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

I created a migration, and the very next command said The migration 'AddProductVersion' was not found. The file was right there in the Migrations folder. The cause was a flag I'd added to save a few seconds: --no-build makes the EF Core tools use the last compiled DLL, and that DLL was older than the migration.