Skip to content

Commit 5dd577f

Browse files
authored
Fix: Value cannot be null parameter name key (#34730)
Fixes #33831
1 parent 94753b1 commit 5dd577f

File tree

7 files changed

+106
-1
lines changed

7 files changed

+106
-1
lines changed

src/EFCore.Relational/Update/ModificationCommand.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,13 @@ void HandleJson(List<IColumnModification> columnModifications)
673673
var processedEntries = new List<IUpdateEntry>();
674674
foreach (var entry in _entries.Where(e => e.EntityType.IsMappedToJson()))
675675
{
676-
var jsonColumn = GetTableMapping(entry.EntityType)!.Table.FindColumn(entry.EntityType.GetContainerColumnName()!)!;
676+
var jsonColumn = GetTableMapping(entry.EntityType)!.Table.FindColumn(entry.EntityType.GetContainerColumnName()!);
677+
678+
if (jsonColumn == null)
679+
{
680+
continue;
681+
}
682+
677683
var jsonPartialUpdateInfo = FindJsonPartialUpdateInfo(entry, processedEntries);
678684

679685
if (jsonPartialUpdateInfo == null)

test/EFCore.Relational.Specification.Tests/Update/JsonUpdateFixtureBase.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,17 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
201201
b.Property(x => x.Name);
202202
});
203203

204+
modelBuilder.Entity<JsonEntityHasComplexChild>().Property(x => x.Id).ValueGeneratedNever();
205+
modelBuilder.Entity<JsonEntityHasComplexChild>().OwnsOne(x => x.EntityReference, b =>
206+
{
207+
b.Property(x => x.Id).ValueGeneratedNever();
208+
b.ToJson();
209+
b.OwnsOne(x => x.AEntityReference, bb =>
210+
{
211+
bb.ToJson();
212+
});
213+
});
214+
204215
base.OnModelCreating(modelBuilder, context);
205216
}
206217

test/EFCore.Relational.Specification.Tests/Update/JsonUpdateTestBase.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,46 @@ public virtual Task Add_entity_with_json()
7777
Assert.Equal("ss2", collectionLeaf[1].SomethingSomething);
7878
});
7979

80+
[ConditionalFact]
81+
public virtual Task Add_owned_entity_with_json()
82+
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
83+
CreateContext,
84+
UseTransaction,
85+
async context =>
86+
{
87+
var newEntity = new JsonEntityHasComplexChild
88+
{
89+
Id = 1,
90+
Name = "NewEntity",
91+
EntityReference = new JsonEntityHasComplexChildForReference
92+
{
93+
Id = 2,
94+
Name = "NewReference",
95+
AEntityReference = new AJsonEntityHasComplexChildForReferenceForReference
96+
{
97+
Id = 3,
98+
Name = "NewReferenceReference"
99+
}
100+
}
101+
};
102+
103+
context.Set<JsonEntityHasComplexChild>().Add(newEntity);
104+
ClearLog();
105+
await context.SaveChangesAsync();
106+
},
107+
async context =>
108+
{
109+
var query = await context.JsonEntitiesHasComplexChild.ToListAsync();
110+
Assert.Equal(1, query.Count);
111+
112+
var newEntity = query.Where(e => e.Id == 1).Single();
113+
Assert.Equal("NewEntity", newEntity.Name);
114+
Assert.Equal(2, newEntity.EntityReference.Id);
115+
Assert.Equal("NewReference", newEntity.EntityReference.Name);
116+
Assert.Equal(3, newEntity.EntityReference.AEntityReference.Id);
117+
Assert.Equal("NewReferenceReference", newEntity.EntityReference.AEntityReference.Name);
118+
});
119+
80120
[ConditionalFact]
81121
public virtual Task Add_entity_with_json_null_navigations()
82122
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.EntityFrameworkCore.TestModels.JsonQuery;
5+
6+
#nullable disable
7+
8+
public class AJsonEntityHasComplexChildForReferenceForReference
9+
{
10+
public int Id { get; set; }
11+
public string Name { get; set; }
12+
13+
public int? ParentId { get; set; }
14+
public JsonEntityHasComplexChildForReference Parent { get; set; }
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.EntityFrameworkCore.TestModels.JsonQuery;
5+
6+
#nullable disable
7+
8+
public class JsonEntityHasComplexChild
9+
{
10+
public int Id { get; set; }
11+
public string Name { get; set; }
12+
13+
public JsonEntityHasComplexChildForReference EntityReference { get; set; }
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.EntityFrameworkCore.TestModels.JsonQuery;
5+
6+
#nullable disable
7+
8+
public class JsonEntityHasComplexChildForReference
9+
{
10+
public int Id { get; set; }
11+
public string Name { get; set; }
12+
13+
// A is required for sorting
14+
public AJsonEntityHasComplexChildForReferenceForReference AEntityReference { get; set; }
15+
16+
public int? ParentId { get; set; }
17+
public JsonEntityHasComplexChild Parent { get; set; }
18+
}

test/EFCore.Specification.Tests/TestModels/JsonQuery/JsonQueryContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class JsonQueryContext(DbContextOptions options) : DbContext(options)
1616
public DbSet<JsonEntityInheritanceBase> JsonEntitiesInheritance { get; set; }
1717
public DbSet<JsonEntityAllTypes> JsonEntitiesAllTypes { get; set; }
1818
public DbSet<JsonEntityConverters> JsonEntitiesConverters { get; set; }
19+
public DbSet<JsonEntityHasComplexChild> JsonEntitiesHasComplexChild { get; set; }
1920

2021
public static Task SeedAsync(JsonQueryContext context)
2122
{

0 commit comments

Comments
 (0)