Skip to main content

290 posts tagged with ".NET"

.NET Framework and Core for Application Development

View All Tags

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.

Fixed: MySQL Rounds a Decimal Like 9.999 to 10.00 Without an Error (Note 1265: Data truncated for column)

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

I sent a product price of 9.999 to my API, and it answered 201 Created with a price of 10.00. No error, no warning: MySQL rounded the amount to fit its DECIMAL(12,2) column, even in strict mode, and raised only a note that EF Core never shows. For money, silently changing the number isn't acceptable, so the API now refuses it.

Fixed: "Data too long for column 'Name' at row 1" When Saving with EF Core and MySQL

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

A long category name made my API answer 500, with Data too long for column 'Name' at row 1 in the log. MySQL was right to refuse it: the column is varchar(100), and the name had 101 characters. The problem was that the request got as far as MySQL at all. A validation attribute stops it first, and the client gets an answer it can act on.

Fixed: "Unknown column 'p.Version' in 'field list'" with EF Core and MySQL

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

Right after I added a property to an EF Core entity, every request that touched that table answered 500, and the log said MySqlException: Unknown column 'p.Version' in 'field list'. The code built without a problem, because the C# was fine. The table simply didn't have that column yet: changing the class doesn't change the database until a migration does.

How to Connect a .NET C# Web API to a MySQL Database — Part 2: Create MySQL Tables with EF Core Migrations

· 45 min read
Jagdish Kumawat
Founder @ Dewiride

Part 1 connected a .NET C# Web API to MySQL, but the database is still empty. This part describes the inventory data as C# classes and lets EF Core migrations create the matching MySQL tables, with the column types, keys, indexes and constraints you'd otherwise write by hand. You'll read the SQL before it runs, apply it, change the model and add a second migration, and then check every table from inside MySQL.

How to Apply EF Core Migrations with a Migrations Bundle (efbundle)

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

dotnet ef database update is fine on your own machine, but a server shouldn't need the .NET SDK, the source code and the EF Core tools just to update its database. A migrations bundle packs your migrations and the code that applies them into one executable you can copy anywhere. This guide builds one, runs it, and then runs it the way Microsoft recommends for deployments, which taught me two things the hard way.

How to Register Services with an IServiceCollection Extension Method in ASP.NET Core (AddXyz Pattern)

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

builder.Services.AddDbContext(...), options binding, validation, retries, health checks: Program.cs fills up fast, and the database setup ends up scattered between other features. ASP.NET Core's own answer is the AddXyz method: AddControllers, AddHealthChecks, AddOpenApi. You can write your own the same way, as an extension method on IServiceCollection. This guide builds one for an EF Core database, calls it with one line, and shows its startup checks catching mistakes.