Skip to content

Fix to #32192 - Remove lookup of JsonTypeMapping via JsonElement #35788

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

Merged
merged 1 commit into from
Mar 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Metadata/Internal/JsonColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public JsonColumn(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override RelationalTypeMapping GetDefaultStoreTypeMapping()
=> (RelationalTypeMapping)Table.Model.Model.GetModelDependencies().TypeMappingSource.FindMapping(typeof(JsonElement))!;
=> (RelationalTypeMapping)Table.Model.Model.GetModelDependencies().TypeMappingSource.FindMapping(typeof(JsonTypePlaceholder))!;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ public JsonColumnBase(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override RelationalTypeMapping GetDefaultStoreTypeMapping()
=> (RelationalTypeMapping)Table.Model.Model.GetModelDependencies().TypeMappingSource.FindMapping(typeof(JsonElement))!;
=> (RelationalTypeMapping)Table.Model.Model.GetModelDependencies().TypeMappingSource.FindMapping(typeof(JsonTypePlaceholder))!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ public JsonViewColumn(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override RelationalTypeMapping GetDefaultStoreTypeMapping()
=> (RelationalTypeMapping)Table.Model.Model.GetModelDependencies().TypeMappingSource.FindMapping(typeof(JsonElement))!;
=> (RelationalTypeMapping)Table.Model.Model.GetModelDependencies().TypeMappingSource.FindMapping(typeof(JsonTypePlaceholder))!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ private static void CreateContainerColumn<TColumnMappingBase>(
{
Check.DebugAssert(tableBase.FindColumn(containerColumnName) == null, $"Table does not have column '{containerColumnName}'.");

var jsonColumnTypeMapping = relationalTypeMappingSource.FindMapping(typeof(JsonElement), storeTypeName: containerColumnType)!;
var jsonColumnTypeMapping = relationalTypeMappingSource.FindMapping(typeof(JsonTypePlaceholder), storeTypeName: containerColumnType)!;
var jsonColumn = createColumn(containerColumnName, containerColumnType, tableBase, jsonColumnTypeMapping);
tableBase.Columns.Add(containerColumnName, jsonColumn);
jsonColumn.IsNullable = !ownership.IsRequiredDependent || !ownership.IsUnique;
Expand Down
3 changes: 1 addition & 2 deletions src/EFCore.Relational/Storage/JsonTypeMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Data;
using System.Text.Json;

namespace Microsoft.EntityFrameworkCore.Storage;

/// <summary>
/// <para>
/// Represents the mapping between a <see cref="JsonElement" /> type and a database type.
/// Represents the mapping between a JSON object and a database type.
/// </para>
/// <para>
/// This type is typically used by database providers (and other extensions). It is generally
Expand Down
14 changes: 14 additions & 0 deletions src/EFCore.Relational/Storage/JsonTypePlaceholder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.Storage;

/// <summary>
/// A type representing CLR type of the JsonTypeMapping.
/// </summary>
public sealed class JsonTypePlaceholder
{
private JsonTypePlaceholder()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private static readonly MethodInfo GetStringMethod
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public SqlServerOwnedJsonTypeMapping(string storeType)
: base(storeType, typeof(JsonElement), System.Data.DbType.String)
: base(storeType, typeof(JsonTypePlaceholder), System.Data.DbType.String)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Collections;
using System.Data;
using System.Text.Json;

namespace Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal;

Expand Down Expand Up @@ -239,7 +238,7 @@ public SqlServerTypeMappingSource(
var clrType = mappingInfo.ClrType;
var storeTypeName = mappingInfo.StoreTypeName;

if (clrType == typeof(JsonElement))
if (clrType == typeof(JsonTypePlaceholder))
{
return storeTypeName == "json"
? SqlServerOwnedJsonTypeMapping.OwnedJsonTypeDefault
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private static readonly ConstructorInfo MemoryStreamConstructor
/// </summary>
/// <param name="storeType">The name of the database type.</param>
public SqliteJsonTypeMapping(string storeType)
: base(storeType, typeof(JsonElement), System.Data.DbType.String)
: base(storeType, typeof(JsonTypePlaceholder), System.Data.DbType.String)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private static readonly HashSet<string> SpatialiteTypes
{ typeof(double), Real },
{ typeof(float), new FloatTypeMapping(RealTypeName) },
{ typeof(Guid), SqliteGuidTypeMapping.Default },
{ typeof(JsonElement), SqliteJsonTypeMapping.Default }
{ typeof(JsonTypePlaceholder), SqliteJsonTypeMapping.Default }
};

private readonly Dictionary<string, RelationalTypeMapping> _storeTypeMappings = new(StringComparer.OrdinalIgnoreCase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#nullable disable

using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json;
using NameSpace1;

namespace Microsoft.EntityFrameworkCore.Query
Expand Down Expand Up @@ -239,6 +240,32 @@ public static DateTime Modify(DateTime date)

#endregion

#region 34752

[ConditionalFact]
public virtual async Task Mapping_JsonElement_property_throws_a_meaningful_exception()
{
var message = (await Assert.ThrowsAsync<InvalidOperationException>(
() => InitializeAsync<Context34752>())).Message;

Assert.Equal(
CoreStrings.PropertyNotAdded(nameof(Context34752.Entity), nameof(Context34752.Entity.Json), nameof(JsonElement)),
message);
}

protected class Context34752(DbContextOptions options) : DbContext(options)
{
public DbSet<Entity> Entities { get; set; }

public class Entity
{
public int Id { get; set; }
public JsonElement Json { get; set; }
}
}

#endregion

#region Inlined redacting

protected abstract DbContextOptionsBuilder SetTranslateParameterizedCollectionsToConstants(DbContextOptionsBuilder optionsBuilder);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// <auto-generated />
using System;
using System.Collections.Generic;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Update.Internal;

#pragma warning disable 219, 612, 618
Expand Down Expand Up @@ -2258,10 +2258,10 @@ private IRelationalModel CreateRelationalModel()
IsNullable = true
};
principalBaseTable.Columns.Add("ManyOwned", manyOwnedColumn);
manyOwnedColumn.Accessors = ColumnAccessorsFactory.CreateGeneric<JsonElement>(manyOwnedColumn);
manyOwnedColumn.Accessors = ColumnAccessorsFactory.CreateGeneric<JsonTypePlaceholder>(manyOwnedColumn);
var ownedColumn = new JsonColumn("Owned", "nvarchar(max)", principalBaseTable);
principalBaseTable.Columns.Add("Owned", ownedColumn);
ownedColumn.Accessors = ColumnAccessorsFactory.CreateGeneric<JsonElement>(ownedColumn);
ownedColumn.Accessors = ColumnAccessorsFactory.CreateGeneric<JsonTypePlaceholder>(ownedColumn);
var refTypeArrayColumn = new Column("RefTypeArray", "nvarchar(max)", principalBaseTable)
{
IsNullable = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// <auto-generated />
using System;
using System.Collections.Generic;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Update.Internal;
using NetTopologySuite.Geometries;

Expand Down Expand Up @@ -2325,10 +2325,10 @@ private IRelationalModel CreateRelationalModel()
IsNullable = true
};
principalBaseTable.Columns.Add("ManyOwned", manyOwnedColumn);
manyOwnedColumn.Accessors = ColumnAccessorsFactory.CreateGeneric<JsonElement>(manyOwnedColumn);
manyOwnedColumn.Accessors = ColumnAccessorsFactory.CreateGeneric<JsonTypePlaceholder>(manyOwnedColumn);
var ownedColumn = new JsonColumn("Owned", "TEXT", principalBaseTable);
principalBaseTable.Columns.Add("Owned", ownedColumn);
ownedColumn.Accessors = ColumnAccessorsFactory.CreateGeneric<JsonElement>(ownedColumn);
ownedColumn.Accessors = ColumnAccessorsFactory.CreateGeneric<JsonTypePlaceholder>(ownedColumn);
var pointColumn0 = new Column("Point", "geometry", principalBaseTable)
{
IsNullable = true
Expand Down