-
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
Columns with name "Id" in JSon object causes System.ArgumentException in materialization #29380
Comments
@maumar Looks like it could be a problem with constructor binding for JSON objects in the materializer. |
@maumar @ajcvickers Other properties of type int is deserialized OK - but when named Id will throw the ArgumentException. Could it be that Id is considered Key by convention for that type? |
@jonnybee Yes, I suspect that it is something to do with that. |
owned types that are part of the collection have hard-coded ordinal key named "Id" of type int. Related issue: #28594 |
we should at least throw in validation until flexible ordinal key is implemented |
Note from triage: we should investigate if this is a materialization bug separate from #28594. Possible workaround if the entity doesn't need the property to be called modelBuilder.Entity<Customer>()
.OwnsOne(customer => customer.Contact, b =>
{
b.ToJson();
b.OwnsMany(e => e.PhoneNumbers, b =>
{
b.Property(e => e.Key).HasJsonPropertyName("Id");
});
}); |
the problem comes from the fact that the Id property is considered to be a key. There doesn't seem to be a separate materialization bug, apart from #28594. |
Hi, I'm also facing the same (or very similar) problem when a property is like:
If I'm not mistaken, entity use this nomenclature to generate reference between classes, so may be is "reserved" too. |
Notes from triage:
|
Is there a way to have this feature on EF 7.0.8? |
According to the Npgsql docs, they've deprecated their support for Please prioritize fixing this - it's been open for more than a year already... |
I ran into this today as well and it took me quite some time to figure it out (found this issue only after having identified the root cause). If this can't be fixed soon I'd suggest adding a warning to the documentation at least. |
Just ran into this issue and spent a couple of hours until I got to this ticket. |
I also encountered this problem today. Microsoft writes everywhere about good JSON support in the EF, but in fact we have many problems that prevent the use of JSON in real applications. We are waiting for a solution from the EFCore team. |
Fixes #29380 There are several aspects to #29380: - Constructor binding (already fixed) - Round-tripping the `Id` property value (addressed by this PR) - Persisting key values in JSON collection documents (tracked by #28594) I investigated persisting key values, but identity lookup requires key values in the materializer before we do the parsing of the document. This means persisted key values are not available without re-writing this part of the shaper, which we can't do for 9. To fix the main issue (round-trip `Id`) this PR changes the way identity on owned JSON collection documents works. Instead of discovering and using the `Id` property, we treat it like any other property. We then create a shadow `__synthesizedOrdinal` property to act as the ordinal part of the key. We need to discuss: - Is this breaking for any scenario that was working before? - Does this put us in a bad place for the future of owned types with explicit keys?
Fixes #29380 There are several aspects to #29380: - Constructor binding (already fixed) - Round-tripping the `Id` property value (addressed by this PR) - Persisting key values in JSON collection documents (tracked by #28594) I investigated persisting key values, but identity lookup requires key values in the materializer before we do the parsing of the document. This means persisted key values are not available without re-writing this part of the shaper, which we can't do for 9. To fix the main issue (round-trip `Id`) this PR changes the way identity on owned JSON collection documents works. Instead of discovering and using the `Id` property, we treat it like any other property. We then create a shadow `__synthesizedOrdinal` property to act as the ordinal part of the key. We need to discuss: - Is this breaking for any scenario that was working before? - Does this put us in a bad place for the future of owned types with explicit keys?
* Persist `Id` values into owned collection JSON documents Fixes #29380 There are several aspects to #29380: - Constructor binding (already fixed) - Round-tripping the `Id` property value (addressed by this PR) - Persisting key values in JSON collection documents (tracked by #28594) I investigated persisting key values, but identity lookup requires key values in the materializer before we do the parsing of the document. This means persisted key values are not available without re-writing this part of the shaper, which we can't do for 9. To fix the main issue (round-trip `Id`) this PR changes the way identity on owned JSON collection documents works. Instead of discovering and using the `Id` property, we treat it like any other property. We then create a shadow `__synthesizedOrdinal` property to act as the ordinal part of the key. We need to discuss: - Is this breaking for any scenario that was working before? - Does this put us in a bad place for the future of owned types with explicit keys? * Added validation of no explicit keys
* Persist `Id` values into owned collection JSON documents Fixes #29380 There are several aspects to #29380: - Constructor binding (already fixed) - Round-tripping the `Id` property value (addressed by this PR) - Persisting key values in JSON collection documents (tracked by #28594) I investigated persisting key values, but identity lookup requires key values in the materializer before we do the parsing of the document. This means persisted key values are not available without re-writing this part of the shaper, which we can't do for 9. To fix the main issue (round-trip `Id`) this PR changes the way identity on owned JSON collection documents works. Instead of discovering and using the `Id` property, we treat it like any other property. We then create a shadow `__synthesizedOrdinal` property to act as the ordinal part of the key. We need to discuss: - Is this breaking for any scenario that was working before? - Does this put us in a bad place for the future of owned types with explicit keys? * Added validation of no explicit keys
* Persist `Id` values into owned collection JSON documents Fixes #29380 There are several aspects to #29380: - Constructor binding (already fixed) - Round-tripping the `Id` property value (addressed by this PR) - Persisting key values in JSON collection documents (tracked by #28594) I investigated persisting key values, but identity lookup requires key values in the materializer before we do the parsing of the document. This means persisted key values are not available without re-writing this part of the shaper, which we can't do for 9. To fix the main issue (round-trip `Id`) this PR changes the way identity on owned JSON collection documents works. Instead of discovering and using the `Id` property, we treat it like any other property. We then create a shadow `__synthesizedOrdinal` property to act as the ordinal part of the key. We need to discuss: - Is this breaking for any scenario that was working before? - Does this put us in a bad place for the future of owned types with explicit keys? * Added validation of no explicit keys
@ajcvickers didn't this go into rc2 as opposed to rc1 (the milestone)? |
Thx for the example @sclarke81. For anyone else still having trouble: the workaround doesn't seem to work if you're using No-Tracking queries, it throws an exception "shadow properties cannot be preserved when attempting to SaveChanges". |
Hello, thx for the fix, when can we expect to see a RC2 Nuget ? |
@GaetanVigner in around a month, |
Using EF Core 7.0.0-rc.2.22472.11 (also same in v7.0.0-rtm.22504.12)
When JSon child object has a column named Id (f.ex of type Int) the materialization will throw:
System.ArgumentException: 'Expression of type 'System.Object' cannot be used for constructor parameter of type 'System.Int32'
Make these modifications to NewInEFCore7 sample in EntityFramework.Docs to reproduce the bug:
When you run the sample it will now throw ArgumentException here:
If I change Id to a property with { get; set; } and no parameters to the CTOR this exception is thrown:
System.ArgumentException: 'Expression of type 'System.Object' cannot be used for assignment to type 'System.Int32''
The text was updated successfully, but these errors were encountered: