-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Fix NRE in AddJsonNavigationBindings when combining DbFunction with OwnsOne/OwnsMany ToJson #37855
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
Changes from 3 commits
1ddc7e7
c55a409
1c902a7
4bfd2db
e3ee4f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2997,6 +2997,9 @@ private static IQueryable<Order> GetOrdersForCustomer(int id) | |
| private static IQueryable<Order> GetOrdersForCustomer(string name) | ||
| => throw new NotImplementedException(); | ||
|
|
||
| private static IQueryable<EntityWithJsonOwnedType> GetEntitiesWithJsonOwnedType() | ||
| => throw new NotImplementedException(); | ||
|
|
||
| [ConditionalFact] | ||
| public void Complex_property_container_column_type_is_used_in_relational_model() | ||
| { | ||
|
|
@@ -3264,6 +3267,36 @@ public void Complex_property_json_column_is_nullable_in_TPH_hierarchy() | |
| Assert.IsType<JsonColumn>(jsonColumn); | ||
| } | ||
|
|
||
| [ConditionalFact] | ||
| public void Can_use_relational_model_with_functions_and_json_owned_types() | ||
|
AndriySvyryd marked this conversation as resolved.
|
||
| { | ||
| var modelBuilder = CreateConventionModelBuilder(); | ||
|
|
||
| modelBuilder.Entity<EntityWithJsonOwnedType>(cb => | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot Try to use existing classes instead of adding EntityWithJsonOwnedType.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 4bfd2db. Replaced |
||
| { | ||
| cb.HasKey(x => x.Id); | ||
| #pragma warning disable EF8001 // Owned JSON entities are obsolete | ||
| cb.OwnsOne(x => x.OwnedData, o => o.ToJson("owned_data")); | ||
| cb.OwnsMany(x => x.OwnedItems, o => o.ToJson("owned_items")); | ||
| #pragma warning restore EF8001 | ||
| }); | ||
|
|
||
| modelBuilder.HasDbFunction( | ||
| typeof(RelationalModelTest).GetMethod( | ||
| nameof(GetEntitiesWithJsonOwnedType), BindingFlags.NonPublic | BindingFlags.Static)); | ||
|
|
||
| var model = Finalize(modelBuilder); | ||
|
|
||
| var entityType = model.Model.FindEntityType(typeof(EntityWithJsonOwnedType)); | ||
|
|
||
| var functionMappings = entityType.GetFunctionMappings().ToList(); | ||
| Assert.Single(functionMappings); | ||
|
|
||
| var storeFunction = functionMappings[0].StoreFunction; | ||
| Assert.NotNull(storeFunction.FindColumn("owned_data")); | ||
| Assert.NotNull(storeFunction.FindColumn("owned_items")); | ||
| } | ||
|
|
||
| private static IRelationalModel Finalize(TestHelpers.TestModelBuilder modelBuilder) | ||
| => modelBuilder.FinalizeModel(designTime: true).GetRelationalModel(); | ||
|
|
||
|
|
@@ -3407,6 +3440,24 @@ private class ComplexData | |
| public string Value { get; set; } | ||
| public int Number { get; set; } | ||
| } | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot Remove blank line
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed in e3ee4f0. |
||
| private class EntityWithJsonOwnedType | ||
| { | ||
| public int Id { get; set; } | ||
| public string Name { get; set; } | ||
| public OwnedJsonData OwnedData { get; set; } | ||
| public ICollection<OwnedJsonItem> OwnedItems { get; set; } | ||
| } | ||
|
|
||
| private class OwnedJsonData | ||
| { | ||
| public string Value { get; set; } | ||
| } | ||
|
|
||
| private class OwnedJsonItem | ||
| { | ||
| public string Description { get; set; } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The annotation-list initialization + mapping registration logic is duplicated (similar logic exists for the root
entityTypeearlier inAddTvfs). Consider extracting this into a small helper to keep the mapping registration consistent and reduce copy/paste (e.g., a local function that “gets/creates function mappings list, adds mapping, and registers it on the StoreFunction”).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot The annotation-list initialization + mapping registration logic is duplicated (similar logic exists for the root entityType earlier in AddTvfs). Consider extracting this into a small helper to keep the mapping registration consistent and reduce duplication
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extracted
AddTvfMappinghelper in e3ee4f0. The duplicated annotation-list initialization + mapping registration logic inAddTvfsis now a single method called for both the root entity and each owned JSON type.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extracted
AddTvfMappinghelper in e3ee4f0.