PartialUpdate ignores bool request property when set False (works when set True)

I use PartialUpdate to change a database flag to true or false via a Patch service:

[Tag(“Account”)]
[ValidateIsAuthenticated]
[ValidateHasRole(“MANAGE.DATA”)]
[Route("/account/contact/{Id}", “PATCH”)]
[AutoApply(DbAuditBehavior.MriAuditPatch)]
public class CloseOpenAccountContact : IPatchDb, IReturn, IPatch
{
public Guid Id { get; init; }
public bool Closed { get; init; }
public ulong RowVersion { get; init; }
}

When the RequestDto contains close is true, everything works fine and the database record is updated correctly.

{
  "id": "b40d74f10476eb11b528482ae316d296",
  "closed": true,
  "rowVersion": 18529
}

However, when the RequestDto contains close is false, the database record is not updated.

{
  "id": "b40d74f10476eb11b528482ae316d296",
  "closed": false,
  "rowVersion": 18667
}

I have dumped the executed SQL and the Update statement is missing the Closed value when it equals false in the RequestDto.

When Closed = true in the RequestDto, Closed is present in the SQL UPDATE statement:

SQL: 
UPDATE "Account"."AccountContact" 
SET 
"Closed"=@Closed, 
"DataEventOn"=@DataEventOn, 
"DataEventBy"=@DataEventBy, 
"DataEventId"=@DataEventId 
WHERE ("Id" = @0 AND "DataEventRv" = @1) 
PARAMS: 
@0=b40d74f1-0476-eb11-b528-482ae316d296, 
@1=18529, 
@Closed=True, 
@DataEventOn=2021-02-23 18:50:38, 
@DataEventBy=e13dda58-d9fc-422a-b903-c5b0ed2570f7, 
@DataEventId=2

SQL: 
INSERT INTO "Log"."Audit" 
("EventType","Model","ModelId","EventDate","RowsUpdated","RequestType","RequestBody","UserAuthId","UserAuthName") 
VALUES 
(@EventType,@Model,@ModelId,@EventDate,@RowsUpdated,@RequestType,@RequestBody,@UserAuthId,@UserAuthName)
PARAMS:
 @EventType=Patch, @Model=AccountContact, @ModelId=b40d74f1-0476-eb11-b528-482ae316d296, 
 @EventDate=2021-02-23 18:50:38, @RowsUpdated=1, @RequestType=CloseOpenAccountContact,
 @RequestBody={"id":"b40d74f10476eb11b528482ae316d296","closed":true,"rowVersion":18529}, 
 @UserAuthId=e13dda58-d9fc-422a-b903-c5b0ed2570f7, @UserAuthName=billy.bob

SQL: 
SELECT "DataEventRv" 
FROM "Account"."AccountContact" 
WHERE "Id" = @0
PARAMS: @0=b40d74f1-0476-eb11-b528-482ae316d296

When Closed = false in the RequestDto, Closed is missing from the SQL UPDATE statement:

SQL: 
UPDATE "Account"."AccountContact" 
SET 
"DataEventOn"=@DataEventOn, 
"DataEventBy"=@DataEventBy, 
"DataEventId"=@DataEventId 
WHERE ("Id" = @0 AND "DataEventRv" = @1)
PARAMS: 
@0=b40d74f1-0476-eb11-b528-482ae316d296, 
@1=18667, @DataEventOn=2021-02-23 18:51:33, 
@DataEventBy=e13dda58-d9fc-422a-b903-c5b0ed2570f7, 
@DataEventId=2

SQL: 
INSERT INTO "Log"."Audit" 
("EventType","Model","ModelId","EventDate","RowsUpdated","RequestType","RequestBody","UserAuthId","UserAuthName") 
VALUES (@EventType,@Model,@ModelId,@EventDate,@RowsUpdated,@RequestType,@RequestBody,@UserAuthId,@UserAuthName)
PARAMS: 
@EventType=Patch, @Model=AccountContact, @ModelId=b40d74f1-0476-eb11-b528-482ae316d296, 
@EventDate=2021-02-23 18:51:33, @RowsUpdated=1, @RequestType=CloseOpenAccountContact,
 @RequestBody={"id":"b40d74f10476eb11b528482ae316d296","closed":false,"rowVersion":18667}, 
 @UserAuthId=e13dda58-d9fc-422a-b903-c5b0ed2570f7, @UserAuthName=billy.bob

SQL: 
SELECT "DataEventRv" 
FROM "Account"."AccountContact" 
WHERE "Id" = @0
PARAMS: @0=b40d74f1-0476-eb11-b528-482ae316d296

Am I misunderstanding something?

I noticed the solution to another question, and also making my RequestDto’s closed property nullable solved my problem: false and true values are handled correctly

public bool? Closed { get; init; }

2 Likes