Skip to content
Open
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
16 changes: 16 additions & 0 deletions src/EFCore.Relational/Diagnostics/RelationalEventId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ private enum Id
KeyPropertiesNotMappedToTable = CoreEventId.RelationalBaseId + 610,
StoredProcedureConcurrencyTokenNotMapped = CoreEventId.RelationalBaseId + 611,
TriggerOnNonRootTphEntity = CoreEventId.RelationalBaseId + 612,
OwnedEntityMappedToJsonCollectionWarning = CoreEventId.RelationalBaseId + 613,

// Update events
BatchReadyForExecution = CoreEventId.RelationalBaseId + 700,
Expand Down Expand Up @@ -1001,6 +1002,21 @@ private static EventId MakeValidationId(Id id)
public static readonly EventId TriggerOnNonRootTphEntity =
MakeValidationId(Id.TriggerOnNonRootTphEntity);

/// <summary>
/// An owned entity type is mapped to JSON as a collection, which uses a synthesized ordinal key. This mapping is obsolete; map
/// the type as a complex type collection instead.
/// </summary>
/// <remarks>
/// <para>
/// This event is in the <see cref="DbLoggerCategory.Model.Validation" /> category.
/// </para>
/// <para>
/// This event uses the <see cref="EntityTypeEventData" /> payload when used with a <see cref="DiagnosticSource" />.
/// </para>
/// </remarks>
public static readonly EventId OwnedEntityMappedToJsonCollectionWarning =
MakeValidationId(Id.OwnedEntityMappedToJsonCollectionWarning);

/// <summary>
/// A foreign key specifies properties which don't map to the related tables.
/// </summary>
Expand Down
34 changes: 34 additions & 0 deletions src/EFCore.Relational/Diagnostics/RelationalLoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3386,6 +3386,40 @@ private static string TpcStoreGeneratedIdentity(EventDefinitionBase definition,
p.Property.Name);
}

/// <summary>
/// Logs the <see cref="RelationalEventId.OwnedEntityMappedToJsonCollectionWarning" /> event.
/// </summary>
/// <param name="diagnostics">The diagnostics logger to use.</param>
/// <param name="entityType">The owned entity type mapped to JSON as a collection.</param>
public static void OwnedEntityMappedToJsonCollectionWarning(
this IDiagnosticsLogger<DbLoggerCategory.Model.Validation> diagnostics,
IEntityType entityType)
{
var definition = RelationalResources.LogOwnedEntityMappedToJsonCollection(diagnostics);

if (diagnostics.ShouldLog(definition))
{
definition.Log(diagnostics, entityType.DisplayName());
}

if (diagnostics.NeedsEventData(definition, out var diagnosticSourceEnabled, out var simpleLogEnabled))
{
var eventData = new EntityTypeEventData(
definition,
OwnedEntityMappedToJsonCollectionWarning,
entityType);

diagnostics.DispatchEventData(definition, eventData, diagnosticSourceEnabled, simpleLogEnabled);
}
}

private static string OwnedEntityMappedToJsonCollectionWarning(EventDefinitionBase definition, EventData payload)
{
var d = (EventDefinition<string>)definition;
var p = (EntityTypeEventData)payload;
return d.GenerateMessage(p.EntityType.DisplayName());
}

/// <summary>
/// Logs the <see cref="RelationalEventId.OptionalDependentWithoutIdentifyingPropertyWarning" /> event.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,15 @@ public abstract class RelationalLoggingDefinitions : LoggingDefinitions
[EntityFrameworkInternal]
public EventDefinitionBase? LogTpcStoreGeneratedIdentity;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
[EntityFrameworkInternal]
public EventDefinitionBase? LogOwnedEntityMappedToJsonCollection;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down
9 changes: 9 additions & 0 deletions src/EFCore.Relational/EFCore.Relational.baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -12756,6 +12756,9 @@
{
"Member": "static readonly Microsoft.Extensions.Logging.EventId OptionalDependentWithoutIdentifyingPropertyWarning"
},
{
"Member": "static readonly Microsoft.Extensions.Logging.EventId OwnedEntityMappedToJsonCollectionWarning"
},
{
"Member": "static readonly Microsoft.Extensions.Logging.EventId PendingModelChangesWarning"
},
Expand Down Expand Up @@ -13486,6 +13489,9 @@
{
"Member": "static void OptionalDependentWithoutIdentifyingPropertyWarning(this Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger<Microsoft.EntityFrameworkCore.DbLoggerCategory.Model.Validation> diagnostics, Microsoft.EntityFrameworkCore.Metadata.IEntityType entityType);"
},
{
"Member": "static void OwnedEntityMappedToJsonCollectionWarning(this Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger<Microsoft.EntityFrameworkCore.DbLoggerCategory.Model.Validation> diagnostics, Microsoft.EntityFrameworkCore.Metadata.IEntityType entityType);"
},
{
"Member": "static void PendingModelChangesWarning(this Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger<Microsoft.EntityFrameworkCore.DbLoggerCategory.Migrations> diagnostics, System.Type contextType);"
},
Expand Down Expand Up @@ -14239,6 +14245,9 @@
{
"Member": "virtual void ValidateOptionalDependents(System.Collections.Generic.IReadOnlyList<Microsoft.EntityFrameworkCore.Metadata.IEntityType> mappedTypes, in Microsoft.EntityFrameworkCore.Metadata.StoreObjectIdentifier table, Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger<Microsoft.EntityFrameworkCore.DbLoggerCategory.Model.Validation> logger);"
},
{
"Member": "virtual void ValidateOwnedEntityMappedToJsonCollection(Microsoft.EntityFrameworkCore.Metadata.IEntityType entityType, Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger<Microsoft.EntityFrameworkCore.DbLoggerCategory.Model.Validation> logger);"
},
{
"Member": "override void ValidatePrimitiveCollection(Microsoft.EntityFrameworkCore.Metadata.IProperty property, Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger<Microsoft.EntityFrameworkCore.DbLoggerCategory.Model.Validation> logger);"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public static class RelationalOwnedNavigationBuilderExtensions
/// </remarks>
/// <param name="builder">The builder for the owned navigation being configured.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
[Obsolete(EFDiagnostics.OwnedJsonObsoleteMessage, DiagnosticId = EFDiagnostics.OwnedJsonObsolete)]
public static OwnedNavigationBuilder ToJson(this OwnedNavigationBuilder builder)
=> builder.ToJson(builder.Metadata.GetNavigation(pointsToPrincipal: false)!.Name);
Comment thread
AndriySvyryd marked this conversation as resolved.

Expand All @@ -37,7 +36,6 @@ public static OwnedNavigationBuilder ToJson(this OwnedNavigationBuilder builder)
/// </remarks>
/// <param name="builder">The builder for the owned navigation being configured.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
[Obsolete(EFDiagnostics.OwnedJsonObsoleteMessage, DiagnosticId = EFDiagnostics.OwnedJsonObsolete)]
public static OwnedNavigationBuilder<TOwnerEntity, TDependentEntity> ToJson<TOwnerEntity, TDependentEntity>(
this OwnedNavigationBuilder<TOwnerEntity, TDependentEntity> builder)
where TOwnerEntity : class
Expand All @@ -55,7 +53,6 @@ public static OwnedNavigationBuilder<TOwnerEntity, TDependentEntity> ToJson<TOwn
/// <param name="builder">The builder for the owned navigation being configured.</param>
/// <param name="jsonColumnName">JSON column name to use.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
[Obsolete(EFDiagnostics.OwnedJsonObsoleteMessage, DiagnosticId = EFDiagnostics.OwnedJsonObsolete)]
public static OwnedNavigationBuilder<TOwnerEntity, TDependentEntity> ToJson<TOwnerEntity, TDependentEntity>(
this OwnedNavigationBuilder<TOwnerEntity, TDependentEntity> builder,
string? jsonColumnName)
Expand All @@ -74,7 +71,6 @@ public static OwnedNavigationBuilder<TOwnerEntity, TDependentEntity> ToJson<TOwn
/// <param name="builder">The builder for the owned navigation being configured.</param>
/// <param name="jsonColumnName">JSON column name to use.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
[Obsolete(EFDiagnostics.OwnedJsonObsoleteMessage, DiagnosticId = EFDiagnostics.OwnedJsonObsolete)]
public static OwnedNavigationBuilder ToJson(
this OwnedNavigationBuilder builder,
string? jsonColumnName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,29 @@ protected override void ValidateEntityType(
ValidateStoredProcedures(entityType, logger);
ValidateContainerColumnType(entityType, logger);
ValidateTphTriggers(entityType, logger);
ValidateOwnedEntityMappedToJsonCollection(entityType, logger);
// TODO: support this for raw SQL and function mappings in #19970 and #21627 and remove the check
ValidateJsonEntityOwnerMappedToTableOrView(entityType, logger);
}

/// <summary>
/// Logs a warning if an owned entity type is mapped to JSON as a collection, which uses a synthesized ordinal key. This mapping
/// is obsolete; the type should be mapped as a complex type collection instead.
/// </summary>
/// <param name="entityType">The entity type to validate.</param>
/// <param name="logger">The logger to use.</param>
protected virtual void ValidateOwnedEntityMappedToJsonCollection(
IEntityType entityType,
IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)
{
if (entityType.IsMappedToJson()
&& entityType.FindPrimaryKey() is { } primaryKey
&& primaryKey.Properties.Any(p => p.IsOrdinalKeyProperty()))
{
logger.OwnedEntityMappedToJsonCollectionWarning(entityType);
}
}

/// <summary>
/// Logs a warning if triggers are defined on a non-root entity type using TPH mapping strategy.
/// </summary>
Expand Down
25 changes: 25 additions & 0 deletions src/EFCore.Relational/Properties/RelationalStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/EFCore.Relational/Properties/RelationalStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,10 @@
<value>The entity type '{entityType}' is an optional dependent using table sharing without any required non shared column that could be used to identify whether the entity exists. If all nullable properties contain a 'null' value in database then an object instance won't be created in the query. Add a required property to create instances with 'null' values for other properties or mark the incoming navigation as required to always create an instance.</value>
<comment>Warning RelationalEventId.OptionalDependentWithoutIdentifyingPropertyWarning string</comment>
</data>
<data name="LogOwnedEntityMappedToJsonCollection" xml:space="preserve">
<value>The entity type '{entityType}' is an owned entity type mapped to JSON as a collection, which uses a synthesized ordinal key. Mapping owned entity collections to JSON is obsolete; map it as a complex type collection or configure a non-shadow key instead. See https://aka.ms/efcore-docs-json-owned-entities for more information.</value>
<comment>Warning RelationalEventId.OwnedEntityMappedToJsonCollectionWarning string</comment>
</data>
<data name="LogPendingModelChanges" xml:space="preserve">
<value>The model for context '{contextType}' has pending changes. Add a new migration before updating the database. See https://aka.ms/efcore-docs-pending-changes.</value>
<comment>Error RelationalEventId.PendingModelChangesWarning string</comment>
Expand Down
4 changes: 0 additions & 4 deletions src/Shared/EFDiagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ internal static class EFDiagnostics
internal const string ToAsyncEnumerableOnQueryable = "EF1004";
internal const string SuppressUninitializedDbSetRule = "EFSPR1001";

// Diagnostics for [Obsolete]
internal const string OwnedJsonObsolete = "EF8001";
internal const string OwnedJsonObsoleteMessage = "ToJson() on owned entities has been obsoleted, please switch to using complex types instead for mapping to JSON. See https://aka.ms/efcore-docs-json-owned-entities for more information, and provide feedback on https://github.com/dotnet/efcore/issues/37290 if the transition causes problems for you.";

// Diagnostics for [Experimental]
internal const string ExperimentalApi = "EF9001";
internal const string ProviderExperimentalApi = "EF9002";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4533,7 +4533,6 @@ protected override void BuildModel(ModelBuilder modelBuilder)
Assert.NotNull(testOwnee.FindCheckConstraint("CK_TestOwnee_TestEnum_Enum_Constraint"));
});

#pragma warning disable EF8001 // Owned JSON entities are obsolete
[Fact]
public virtual void Owned_types_mapped_to_json_are_stored_in_snapshot()
=> Test(
Expand Down Expand Up @@ -4772,7 +4771,6 @@ public virtual void Owned_types_mapped_to_json_with_explicit_column_type_are_sto
Assert.Equal("EntityWithTwoProperties", ownedType1.GetContainerColumnName());
Assert.Equal("json", ownedType1.GetContainerColumnType());
});
#pragma warning restore EF8001 // Owned JSON entities are obsolete

private class Order
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public abstract class RelationalFixtureBase : FixtureBase
public TestSqlLoggerFactory TestSqlLoggerFactory
=> (TestSqlLoggerFactory)ListLoggerFactory;

#pragma warning disable EF8001 // Owned JSON entities are obsolete
protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext context)
{
base.OnModelCreating(modelBuilder, context);
Expand Down Expand Up @@ -86,6 +85,5 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
});
}
}
#pragma warning restore EF8001 // Owned JSON entities are obsolete
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ public virtual Task Create_table_with_computed_column(bool? stored)
}
});

#pragma warning disable EF8001 // Owned JSON entities are obsolete
[Fact]
public virtual async Task Create_table_with_json_column()
=> await Test(
Expand Down Expand Up @@ -473,7 +472,6 @@ public virtual async Task Create_table_with_complex_properties_with_nested_colle
table.Columns.Single(c => c.Name == "Id"),
Assert.Single(table.PrimaryKey!.Columns));
});
#pragma warning restore EF8001 // Owned JSON entities are obsolete

[Fact]
public virtual Task Alter_table_add_comment()
Expand Down Expand Up @@ -571,7 +569,6 @@ public virtual Task Rename_table_with_primary_key()
Assert.Equal("Persons", table.Name);
});

#pragma warning disable EF8001 // Owned JSON entities are obsolete
[Fact]
public virtual async Task Rename_table_with_json_column()
=> await Test(
Expand Down Expand Up @@ -664,7 +661,6 @@ public virtual async Task Rename_table_with_json_column()
table.Columns.Single(c => c.Name == "Id"),
Assert.Single(table.PrimaryKey!.Columns));
});
#pragma warning restore EF8001 // Owned JSON entities are obsolete

[Fact]
public virtual Task Move_table()
Expand Down Expand Up @@ -781,7 +777,6 @@ protected class Owned
public int Foo { get; set; }
}

#pragma warning disable EF8001 // Owned JSON entities are obsolete
[Fact]
public virtual async Task Add_json_columns_to_existing_table()
=> await Test(
Expand Down Expand Up @@ -869,7 +864,6 @@ public virtual async Task Add_json_columns_to_existing_table()
table.Columns.Single(c => c.Name == "Id"),
Assert.Single(table.PrimaryKey!.Columns));
});
#pragma warning restore EF8001 // Owned JSON entities are obsolete

[Theory, InlineData(true), InlineData(false), InlineData(null)]
public virtual Task Add_column_with_computedSql(bool? stored)
Expand Down Expand Up @@ -1435,7 +1429,6 @@ public virtual Task Alter_column_reset_collation()
Assert.Null(nameColumn.Collation);
});

#pragma warning disable EF8001 // Owned JSON entities are obsolete
[Fact]
public virtual async Task Convert_json_entities_to_regular_owned()
=> await Test(
Expand Down Expand Up @@ -1732,7 +1725,6 @@ await Test(

AssertSql();
}
#pragma warning restore EF8001 // Owned JSON entities are obsolete

[Fact]
public virtual Task Drop_column()
Expand Down Expand Up @@ -1780,7 +1772,6 @@ public virtual Task Drop_column_computed_and_non_computed_with_dependency()
Assert.Equal("Id", Assert.Single(table.Columns).Name);
});

#pragma warning disable EF8001 // Owned JSON entities are obsolete
[Fact]
public virtual async Task Drop_json_columns_from_existing_table()
=> await Test(
Expand Down Expand Up @@ -1847,7 +1838,6 @@ public virtual async Task Drop_json_columns_from_existing_table()
table.Columns.Single(c => c.Name == "Id"),
Assert.Single(table.PrimaryKey!.Columns));
});
#pragma warning restore EF8001 // Owned JSON entities are obsolete

[Fact]
public virtual Task Rename_column()
Expand All @@ -1862,7 +1852,6 @@ public virtual Task Rename_column()
Assert.Single(table.Columns, c => c.Name == "SomeOtherColumn");
});

#pragma warning disable EF8001 // Owned JSON entities are obsolete
[Fact]
public virtual async Task Rename_json_column()
=> await Test(
Expand Down Expand Up @@ -1953,7 +1942,6 @@ public virtual async Task Rename_json_column()
table.Columns.Single(c => c.Name == "Id"),
Assert.Single(table.PrimaryKey!.Columns));
});
#pragma warning restore EF8001 // Owned JSON entities are obsolete

[Fact]
public virtual Task Create_index()
Expand Down
Loading
Loading