I'm working on a GraphQL API project where user is able to update an entire article or portions of it.
Usually, I just send JSON with columns I want to update and it works flawlessly for these columns, the json is as follows:
{
title: "New title",
categoryId: 3
}
But here's the issue: any other column that I didn't provide in this json becomes a null and simply adding:
if(value == null)
-- don't update that column
Wouldn't work with columns that can be nullable, because there's no difference between sending:
{
title: "New title",
categoryId: 3
}
and sending
{
title: "New title",
categoryId: 3,
someCol: null
}
Even though in first JSON I want the value to remain unchanged and in second JSON I want it to be set to null.
So, my question is - how can I handle it in EFCore?
We have the same situation and we've gone with: null = don't change, anything else = change to value.
var entity = ReadEntity();
if(jsonInput.Field1 != null)
{
entity.Field1 = jsonInput.Field1;
}
This means that you can never 'unset' a field in the entity, but for us, that is acceptable. null = uninitialise, once it's initialised, it can't be 'uninitialised', only set to another value (including 'default').
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With