Video version: https://youtu.be/5kdO9W5FU5w

204, and nothing saved

app.MapPut("/orders/{id}/status", async (int id, StatusDto dto, ShopDb db) => { var order = await db.Orders .FirstOrDefaultAsync(o => o.Id == id); if (order is null) return Results.NotFound(); order.Status = dto.Status; await db.SaveChangesAsync(); return Results.NoContent(); }); 

This endpoint returns 204. The status in the database never changes. It finds the order, sets the new status, and saves. No exception.

The API answers 204 No Content. As far as HTTP is concerned, it worked. The Angular screen shows a green saved message, and the user moves on. The database still holds the old status. And SaveChanges returned zero.

This video is about why that happens, a quick fix that makes it worse, and four checks that catch both.

Why nothing was sent

SaveChanges does not look at your variables. It asks the change tracker what changed. If the tracker never saw the order, there is nothing to save. No UPDATE is sent, and the result is zero.

builder.Services.AddDbContext<ShopDb>(options => options.UseSqlServer(connectionString) .UseQueryTrackingBehavior( QueryTrackingBehavior.NoTracking)); 

The endpoint has no AsNoTracking anywhere. So look at where the DbContext is registered. This line makes every query in the application a no tracking query, unless the query asks otherwise.

With tracking, EF Core keeps a snapshot of the order and finds the changed status. One UPDATE goes out. With no tracking, there is no snapshot to compare against. SaveChanges finds nothing and returns zero.

Teams set this default for a real reason. The documentation says no tracking queries are generally quicker. Most endpoints only read, so everything looks fine. The few that write keep returning 204.

The same silent zero happens with AsNoTracking on a single query, or with the global default. Or when the entity came from a different DbContext, like one created in a background job. That other context saw the order. The one you call SaveChanges on never did.

The quick fix that saves too much

app.MapPut("/orders/{id}", async ( int id, OrderDto dto, ShopDb db) => { var order = new Order { Id = id, Status = dto.Status, Note = dto.Note }; db.Orders.Update(order); await db.SaveChangesAsync(); return Results.NoContent(); }); 

Here is a common way to update a row: build an order from the request. Then call Update and save. Now the status really changes, every time.

public class Order { public int Id { get; set; } public string Status { get; set; } = ""; public string? Note { get; set; } public decimal Discount { get; set; } public string? ApprovedBy { get; set; } } 

But Update sends every property of the entity, not only the ones the request carried. The form sent status and note. Discount and approved by were never set, so they go in as zero and null.

The status changed, as intended. The discount went from fifteen to zero. The approval was wiped to null. Still 204, still no exception. The damage is in the columns nobody was looking at.

A column with a foreign key or a check constraint may reject the default and throw an error. A column without one just takes the zero or the null, and nobody hears about it.

Four checks that catch both

var order = await db.Orders .AsTracking() .FirstOrDefaultAsync(o => o.Id == id); if (order is null) return Results.NotFound(); order.Status = dto.Status; var written = await db.SaveChangesAsync(); if (written != 1) logger.LogWarning("Order {Id}: expected 1 change, saved {N}", id, written); return Results.NoContent(); 

Check one is for endpoints that load a row before they change it. Ask for tracking explicitly. AsTracking overrides a no tracking default for that one query. Then read the number SaveChanges returns. You expected one change. If you got zero, say so.

One catch. If the status was already Delivered, nothing changed, and zero is the right answer. Decide what zero means for that endpoint before you turn it into an error.

var rows = await db.Orders .Where(o => o.Id == id) .ExecuteUpdateAsync(s => s .SetProperty(o => o.Status, dto.Status)); return rows == 0 ? Results.NotFound() : Results.NoContent(); 

Check two. When you only set a column, skip loading. ExecuteUpdate sends one UPDATE, with only that column. It returns the number of rows affected. Zero means no order with that id, so the client gets 404.

{ "Logging": { "LogLevel": { "Default": "Warning", "Microsoft.EntityFrameworkCore.Database.Command": "Information" } } } 

Check three. In development, log the commands EF Core sends to the database. Turn this category on, and a save that sends nothing shows up as exactly that: no command.

[Fact] public async Task Put_status_updates_the_row() { var client = _factory.CreateClient(); var response = await client.PutAsJsonAsync( "/orders/42/status", new { status = "Delivered" }); Assert.Equal(HttpStatusCode.NoContent, response.StatusCode); using var scope = _factory.Services.CreateScope(); var db = scope.ServiceProvider.GetRequiredService<ShopDb>(); var order = await db.Orders.FindAsync(42); Assert.Equal("Delivered", order!.Status); } 

Check four. A test that only asserts 204 passes for both bugs in this article. Read the row back in a fresh scope and assert the values. That test fails on day one.

What to take away

SaveChanges saves only what the tracker saw. Without tracking, a write quietly does nothing. Update on an object built from a request writes every column, even the unsent ones. Read the count EF Core returns. Test the row, not the status code.

References