-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Json: error when trying to materialize json entity with nullable property that is missing in the json string - should materialize the property as null instead #29219
Comments
#29199 The exceptions are similar, but the trigger points are different |
translation: When querying a set of data, if the type contains a JSON column, when the specific attribute of the JSON column does not exist, the query result is bound to the entity and an error will be reported (,,,) The row of data with Id=1 is historical data. The JSON object does not contain the Address field when it is inserted, so when the entity object is updated (the Address field is added to the Author object), the following error is thrown when querying this record: (...) I think the Address property is not serialized to JSON and {Address:null ...} should be equivalent and should not throw an exception! |
👍 That's what I would expect when migrating from a Value Conversion based json column. |
It is enough to use In addition used |
…with missing properties Resolves #29219
#29214 - Json column: constructor with optional enum-parameter throws ArgumentException When producing shaper code for json nullable properties with converters we check if the converter handles null, if not we need to peek into the json value and only pass it thru converter if it's not null, and return null otherwise #29217 - Json: nullable enums on entities mapped to json don't use enum-to-int converter, rather than string, like for non-nullable enums In convention code unwrap nullable type when testing for enum properties that we then apply converter to string #29225 - Json: projecting nullable scalar produces debug.assert in SqlExpression ctor Unwrap nullable type from property before passing it as type to SqlExpression ctor #29219 - Json: error when trying to materialize json entity with nullable property that is missing in the json string - should materialize the property as null instead Modify ExtractJsonProperty method so that it used TryGetProperty when trying to extract nullable properties. Fixes #29214 Fixes #29217 Fixes #29225 Fixes #29219
#29214 - Json column: constructor with optional enum-parameter throws ArgumentException When producing shaper code for json nullable properties with converters we check if the converter handles null, if not we need to peek into the json value and only pass it thru converter if it's not null, and return null otherwise #29217 - Json: nullable enums on entities mapped to json don't use enum-to-int converter, rather than string, like for non-nullable enums In convention code unwrap nullable type when testing for enum properties that we then apply converter to string #29225 - Json: projecting nullable scalar produces debug.assert in SqlExpression ctor Unwrap nullable type from property before passing it as type to SqlExpression ctor #29219 - Json: error when trying to materialize json entity with nullable property that is missing in the json string - should materialize the property as null instead Modify ExtractJsonProperty method so that it used TryGetProperty when trying to extract nullable properties. Fixes #29214 Fixes #29217 Fixes #29225 Fixes #29219
#29214 - Json column: constructor with optional enum-parameter throws ArgumentException When producing shaper code for json nullable properties with converters we check if the converter handles null, if not we need to peek into the json value and only pass it thru converter if it's not null, and return null otherwise #29217 - Json: nullable enums on entities mapped to json don't use enum-to-int converter, rather than string, like for non-nullable enums In convention code unwrap nullable type when testing for enum properties that we then apply converter to string #29225 - Json: projecting nullable scalar produces debug.assert in SqlExpression ctor Unwrap nullable type from property before passing it as type to SqlExpression ctor #29219 - Json: error when trying to materialize json entity with nullable property that is missing in the json string - should materialize the property as null instead Modify ExtractJsonProperty method so that it used TryGetProperty when trying to extract nullable properties. Fixes #29214 Fixes #29217 Fixes #29225 Fixes #29219
#29214 - Json column: constructor with optional enum-parameter throws ArgumentException When producing shaper code for json nullable properties with converters we check if the converter handles null, if not we need to peek into the json value and only pass it thru converter if it's not null, and return null otherwise #29217 - Json: nullable enums on entities mapped to json don't use enum-to-int converter, rather than string, like for non-nullable enums In convention code unwrap nullable type when testing for enum properties that we then apply converter to string #29225 - Json: projecting nullable scalar produces debug.assert in SqlExpression ctor Unwrap nullable type from property before passing it as type to SqlExpression ctor #29219 - Json: error when trying to materialize json entity with nullable property that is missing in the json string - should materialize the property as null instead Modify ExtractJsonProperty method so that it used TryGetProperty when trying to extract nullable properties. Fixes #29214 Fixes #29217 Fixes #29225 Fixes #29219
fixed in d4c22b2 |
@maumar Thanks for fixing the above issue. Unfortunately, I'm facing a similar issue when an object is missing from the JSON document. Lets say the Address was not a string and is an actual class representing different fields of an address. public class Contact
{
public int Id{ get; set; }
public ContactDetails? Contact { get; set; }
}
public class ContactDetails
{
public string? Email { get; set; }
public string? Phone { get; set; }
public Address? Address { get; set; }
}
public class Address
{
public string? Street { get; set; }
public string? City { get; set; }
public string? PostCode { get; set; }
public string? State { get; set; }
public string? Country { get; set; }
} Configuration public class ContactConfigurations : IEntityTypeConfiguration<Contact>
{
public void Configure(EntityTypeBuilder<Contact> builder)
{
builder.ToTable("tbl_contacts", "member");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.HasColumnName("id")
.IsRequired();
builder.OwnsOne(x => x.Contact, b =>
{
b.ToJson("contact_details");
b.OwnsOne(x => x.Address);
});
}
} Data
var contact1 = await _dbContext.Contacts.SingleOrDefaultAsync(x => x.id == 1); // error with the below exception
var contact2 = await _dbContext.Contacts.SingleOrDefaultAsync(x => x.id == 2); // success
var contact3 = await _dbContext.Contacts.SingleOrDefaultAsync(x => x.id == 3); // success
var contact4 = await _dbContext.Contacts.SingleOrDefaultAsync(x => x.id == 3); // success Exception
EF Core version: 7.0.0 |
@imranmomin Have you found out anything regarding that or created a new separate issue given this one is closed? We just stumbled into that unexpected behavior as well. |
@rvplauborg I have not created any separate issue for it. Thanks for doing it. |
File a bug
Include your code
OnModelCreating :
Include stack traces
Id=1 的这行数据是历史数据 插入时 JSON对象并不包含
Address
字段, 所以当更新实体对象后 (Author对象新增 Address 字段 ),查询这条记录抛出如下错误:我认为
Address
属性没有序列化到JSON和{Address:null ...}
应该是等价的 而不应该抛出异常!Include provider and version information
EF Core version: 7.0.0-rc.1.22426.7
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 6.0)
The text was updated successfully, but these errors were encountered: