-
Notifications
You must be signed in to change notification settings - Fork 3.2k
WriteJson: Object reference not set to an instance of an object.
#35530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@douglasg14b can you please take the time to submit a runnable, minimal code sample (please always do that)? The above contains references to types which aren't included (e.g. EnrichedReqResData). You're effectively asking us to guess the missing parts of your code sample. |
No repro was provided (yet). Closing. Will reopen if repro is provided. |
I think I have a reproduction here: https://github.com/chklauser/ReproNreJsonElement/tree/a8dbeb960a6b3fcbd4702a4bedc7b48669823488 Any hints on how to work around this issue until a fix is available would be most welcome 😄 @cincuranet / @roji would this be sufficient to re-open the ticket? |
Yep. That works. |
Minimal repro: using System.Text.Json;
using Microsoft.EntityFrameworkCore;
await using var db = new MyDbContext();
db.Database.EnsureCreated();
db.RootEntities.Add(new()
{
Owned = new()
{
Untyped = JsonSerializer.SerializeToElement(new { x = 5, y = 7 })
}
});
await db.SaveChangesAsync();
class RootEntity
{
public long Id { get; set; }
public OwnedEntity? Owned { get; set; }
}
class OwnedEntity
{
public JsonElement? Untyped { get; set; }
}
class MyDbContext : DbContext
{
public DbSet<RootEntity> RootEntities { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=test.db");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<RootEntity>().OwnsOne(r => r.Owned,
o => o.ToJson());
}
} Exception:
|
dupe of #32192 now we throw much better exception:
|
Bug description
Small model, trying to add a new entity. EF Core odly enough throws a null ref exception.
The last stack frame is at:
The result of
property.GetJsonValueReaderWriter() ?? property.GetTypeMapping().JsonValueReaderWriter;
is null for a dictionary?Your code
Stack traces
Verbose output
EF Core version
9.0.1
Database provider
Npgsql.EntityFrameworkCore.PostgreSQL
Target framework
.NET 9
Operating system
Windows 10
IDE
Rider
The text was updated successfully, but these errors were encountered: