sneki
sneki
CC#
Created by sneki on 9/11/2024 in #help
✅ Possible null reference assignment warning on event unsubscribe
On the following code gives me a CS8601: Possible null reference assignment. on build.
// View.razor.cs
internal EventHandler<FocusSectionEvent> FocusSectionRequested = delegate { };

// Section.razor.cs
[CascadingParameter] public View View { get; set; } = default!;

protected override void OnInitialized() => View.FocusSectionRequested += OnFocusSection; // No warning
public void Dispose() => View.FocusSectionRequested -= OnFocusSection; // warning CS8601: Possible null reference assignment.

private void OnFocusSection(object? sender, FocusSectionEvent e) { /* stuff */ }
// View.razor.cs
internal EventHandler<FocusSectionEvent> FocusSectionRequested = delegate { };

// Section.razor.cs
[CascadingParameter] public View View { get; set; } = default!;

protected override void OnInitialized() => View.FocusSectionRequested += OnFocusSection; // No warning
public void Dispose() => View.FocusSectionRequested -= OnFocusSection; // warning CS8601: Possible null reference assignment.

private void OnFocusSection(object? sender, FocusSectionEvent e) { /* stuff */ }
From what I can gather, it thinks that View.FocusSectionRequested is null, but checking that with an if statement just gives me an 'expression is always true' warning.
PS $> dotnet --info
.NET SDK:
Version: 8.0.206
Commit: bb12410699
Workload version: 8.0.200-manifests.afa40ca5
PS $> dotnet --info
.NET SDK:
Version: 8.0.206
Commit: bb12410699
Workload version: 8.0.200-manifests.afa40ca5
How can I get rid of this warning? Just pragma?
18 replies
CC#
Created by sneki on 1/3/2024 in #help
Npgsql 8.0 + EFCore 8.0 + JSON Colums with type List<T> not working
Hello! I'm not sure if this is a bug or intended behavior, but I'm trying to have a Patient entity that can have a dynamic amount of phone numbers by making its property public List<PhoneNumber> PhoneNumbers { get; set; } = new(); a json column. According to the docs, in version 8 [ColumnType("jsonb")] has been deprecated in favor of using .ToJson(), which I'm now trying to apply since I just upgraded from net7 and Npgsql 7 to 8. I'm setting up EnableDynamicJson() in my DbContext as follows:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
dataSourceBuilder.EnableDynamicJson();
dataSourceBuilder.UseJsonNet(); // just an experiment that didn't work, but doesn't make a difference
var dataSource = dataSourceBuilder.Build();

optionsBuilder
.UseNpgsql(dataSource)
.UseSnakeCaseNamingConvention();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
dataSourceBuilder.EnableDynamicJson();
dataSourceBuilder.UseJsonNet(); // just an experiment that didn't work, but doesn't make a difference
var dataSource = dataSourceBuilder.Build();

optionsBuilder
.UseNpgsql(dataSource)
.UseSnakeCaseNamingConvention();
}
Then, as per the docs, I'm setting up my PatientConfiguration as follows:
public void Configure(EntityTypeBuilder<Patient> builder)
{
// .. noise

builder.OwnsOne(p => p.PhoneNumbers, config =>
{
config.ToJson("phone_numbers");
});

// .. more noise
}
public void Configure(EntityTypeBuilder<Patient> builder)
{
// .. noise

builder.OwnsOne(p => p.PhoneNumbers, config =>
{
config.ToJson("phone_numbers");
});

// .. more noise
}
This all compiles and runs, but when I enter data into this column the value of the column is
select phone_numbers from patients
select phone_numbers from patients
{"Capacity": 4} I already tried
builder.OwnsMany(p => p.PhoneNumbers, config =>
{
config.ToJson("phone_numbers");
config.WithOwner(pn => pn.Patient);
});
builder.OwnsMany(p => p.PhoneNumbers, config =>
{
config.ToJson("phone_numbers");
config.WithOwner(pn => pn.Patient);
});
As @viceroypenguin | 🦋🐧 suggested, however this just gives a
Entity 'PhoneNumber' is mapped to JSON and also to a table or view 'phone_number', but its owner 'Patient' is mapped to a different table or view 'patients'. Every entity mapped to JSON must also map to the same table or view as its owner.
when initializing the database. Is this scenario still supported without a wrapper class, which in my opinion is quite an ugly workaround since this is quite the nice use case for a JSON column?
1 replies