C
C#4mo ago
Tost

(SQL Server) EF Core 8 not respecting JsonIgnore

Hello, I have a json complex property that is being translated to json with ToJson() , some of the properties of the complex property can be null. Now, I want those nullable properties when they are null, not to be written to the json representation. I already tried to use JsonIgnoreAttribute with Condition = JsonIgnoreCondition.WhenWritingNull but EF Core ignores them for some reason. Is there a special attribute that I can use or Fluent API for this behavior? (model) https://pastebin.com/qHDFT4VT
Pastebin
public class Feedback{ public int Id { get; set; } public str...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
7 Replies
SwaggerLife
SwaggerLife4mo ago
@Tost.NET
// Add controllers with views and configure JSON serialization options.
builder.Services.AddControllersWithViews()
.AddJsonOptions(x =>
{
x.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});
// Add controllers with views and configure JSON serialization options.
builder.Services.AddControllersWithViews()
.AddJsonOptions(x =>
{
x.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});
This should do the trick!
Tost
Tost4mo ago
Sadly that doesn't work as EF Core doesn't respect STJ options. I found a similar issue thread on github tho so hopefully it'll be on EF9 resolved: https://github.com/dotnet/efcore/issues/26981
GitHub
Cosmos: allow excluding null values from Json document · Issue #269...
When using [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] [JsonPropertyName("age")] public int? Age { get; set; } This still will create null when saving data in Cosmos Db ...
Tost
Tost4mo ago
No description
No description
SwaggerLife
SwaggerLife4mo ago
@Tost.NET I'm using ef core though. How does your dto look like?
Tost
Tost4mo ago
Pastebin
public class Feedback{ public int Id { get; set; } public str...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
SwaggerLife
SwaggerLife4mo ago
That's very weird. It's working fine for me. You have set the properties to nullable. I don't really get it. Let me check, If I have added some more configuration. @Tost.NET Does cosmos return null as the actual value?
Tost
Tost4mo ago
yes, in the picture provided above, I explicitly made "ClarityId" to be null, and it still persists it in the db sorry for late reply, was on a meeting