Skip to content
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

Metadata work for json. #28540

Merged
merged 1 commit into from
Aug 5, 2022
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
20 changes: 19 additions & 1 deletion src/EFCore.Relational/Design/AnnotationCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public class AnnotationCodeGenerator : IAnnotationCodeGenerator
RelationalAnnotationNames.InsertStoredProcedure,
RelationalAnnotationNames.UpdateStoredProcedure,
RelationalAnnotationNames.MappingFragments,
RelationalAnnotationNames.RelationalOverrides
RelationalAnnotationNames.RelationalOverrides,
RelationalAnnotationNames.JsonColumnTypeMapping
};

#region MethodInfos
Expand Down Expand Up @@ -136,6 +137,10 @@ private static readonly MethodInfo IndexHasFilterNameMethodInfo
= typeof(RelationalIndexBuilderExtensions).GetRuntimeMethod(
nameof(RelationalIndexBuilderExtensions.HasFilter), new[] { typeof(IndexBuilder), typeof(string) })!;

private static readonly MethodInfo ToJsonMethodInfo
= typeof(RelationalOwnedNavigationBuilderExtensions).GetRuntimeMethod(
nameof(RelationalOwnedNavigationBuilderExtensions.ToJson), new[] { typeof(OwnedNavigationBuilder), typeof(string) })!;

#endregion MethodInfos

/// <summary>
Expand Down Expand Up @@ -303,6 +308,19 @@ public virtual IReadOnlyList<MethodCallCodeFragment> GenerateFluentApiCalls(
}
}

if (annotations.TryGetValue(RelationalAnnotationNames.JsonColumnName, out var jsonColumnNameAnnotation)
&& jsonColumnNameAnnotation != null && jsonColumnNameAnnotation.Value is string jsonColumnName
&& entityType.IsOwned())
{
methodCallCodeFragments.Add(
new MethodCallCodeFragment(
ToJsonMethodInfo,
jsonColumnName));

annotations.Remove(RelationalAnnotationNames.JsonColumnName);
annotations.Remove(RelationalAnnotationNames.JsonColumnTypeMapping);
}

methodCallCodeFragments.AddRange(GenerateFluentApiCallsHelper(entityType, annotations, GenerateFluentApi));

return methodCallCodeFragments;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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.Metadata.Internal
{
/// <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>
public static class RelationalPropertyInternalExtensions
{
/// <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>
public static bool IsOrdinalKeyProperty(this IReadOnlyProperty property)
=> property.FindContainingPrimaryKey() is IReadOnlyKey key
&& key.Properties.Count > 1
&& !property.IsForeignKey()
&& property.IsShadowProperty()
&& property.ClrType == typeof(int)
&& property.GetJsonPropertyName() == null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static class RelationalEntityTypeExtensions

var ownership = entityType.FindOwnership();
if (ownership != null
&& ownership.IsUnique)
&& (ownership.IsUnique || entityType.IsMappedToJson()))
{
return ownership.PrincipalEntityType.GetTableName();
}
Expand Down Expand Up @@ -316,7 +316,7 @@ public static IEnumerable<ITableMapping> GetTableMappings(this IEntityType entit

var ownership = entityType.FindOwnership();
return ownership != null
&& ownership.IsUnique
&& (ownership.IsUnique || entityType.IsMappedToJson())
? ownership.PrincipalEntityType.GetViewName()
: null;
}
Expand Down Expand Up @@ -1385,7 +1385,7 @@ public static IEnumerable<IReadOnlyForeignKey> FindRowInternalForeignKeys(
StoreObjectIdentifier storeObject)
{
var primaryKey = entityType.FindPrimaryKey();
if (primaryKey == null)
if (primaryKey == null || entityType.IsMappedToJson())
{
yield break;
}
Expand Down Expand Up @@ -1855,4 +1855,96 @@ public static IEnumerable<ITrigger> GetDeclaredTriggers(this IEntityType entityT
=> Trigger.GetDeclaredTriggers(entityType).Cast<ITrigger>();

#endregion Trigger

#region Json

/// <summary>
/// Gets a value indicating whether the specified entity is mapped to a JSON column.
/// </summary>
/// <param name="entityType">The entity type.</param>
/// <returns>A value indicating whether the associated entity type is mapped to a JSON column.</returns>
public static bool IsMappedToJson(this IReadOnlyEntityType entityType)
maumar marked this conversation as resolved.
Show resolved Hide resolved
=> !string.IsNullOrEmpty(entityType.GetJsonColumnName());

/// <summary>
/// Sets the name of the JSON column to which the entity type is mapped.
/// </summary>
/// <param name="entityType">The entity type to set the JSON column name for.</param>
/// <param name="columnName">The name to set.</param>
public static void SetJsonColumnName(this IMutableEntityType entityType, string? columnName)
=> entityType.SetOrRemoveAnnotation(RelationalAnnotationNames.JsonColumnName, columnName);

/// <summary>
/// Sets the name of the JSON column to which the entity type is mapped.
/// </summary>
/// <param name="entityType">The entity type to set the JSON column name for.</param>
/// <param name="columnName">The name to set.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>The configured value.</returns>
public static string? SetJsonColumnName(
this IConventionEntityType entityType,
string? columnName,
bool fromDataAnnotation = false)
=> (string?)entityType.SetAnnotation(RelationalAnnotationNames.JsonColumnName, columnName, fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the JSON column name.
/// </summary>
/// <param name="entityType">The entity type to set the JSON column name for.</param>
/// <returns>The <see cref="ConfigurationSource" /> for the JSON column name.</returns>
public static ConfigurationSource? GetJsonColumnNameConfigurationSource(this IConventionEntityType entityType)
=> entityType.FindAnnotation(RelationalAnnotationNames.JsonColumnName)
?.GetConfigurationSource();

/// <summary>
/// Gets the JSON column name to which the entity type is mapped.
/// </summary>
/// <param name="entityType">The entity type to get the JSON column name for.</param>
/// <returns>The JSON column name to which the entity type is mapped.</returns>
public static string? GetJsonColumnName(this IReadOnlyEntityType entityType)
=> entityType.FindAnnotation(RelationalAnnotationNames.JsonColumnName)?.Value is string jsonColumnName
? jsonColumnName
: (entityType.FindOwnership()?.PrincipalEntityType.GetJsonColumnName());

/// <summary>
/// Sets the type mapping for the JSON column to which the entity type is mapped.
/// </summary>
/// <param name="entityType">The entity type to set the JSON column type mapping for.</param>
/// <param name="typeMapping">The type mapping to set.</param>
public static void SetJsonColumnTypeMapping(this IMutableEntityType entityType, RelationalTypeMapping typeMapping)
maumar marked this conversation as resolved.
Show resolved Hide resolved
=> entityType.SetOrRemoveAnnotation(RelationalAnnotationNames.JsonColumnTypeMapping, typeMapping);

/// <summary>
/// Sets the type mapping for the JSON column to which the entity type is mapped.
/// </summary>
/// <param name="entityType">The entity type to set the JSON column type mapping for.</param>
/// <param name="typeMapping">The type mapping to set.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>The configured value.</returns>
public static RelationalTypeMapping? SetJsonColumnTypeMapping(
this IConventionEntityType entityType,
RelationalTypeMapping? typeMapping,
bool fromDataAnnotation = false)
=> (RelationalTypeMapping?)entityType.SetAnnotation(RelationalAnnotationNames.JsonColumnTypeMapping, typeMapping, fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the JSON column type mapping.
/// </summary>
/// <param name="entityType">The entity type to set the JSON column type mapping for.</param>
/// <returns>The <see cref="ConfigurationSource" /> for the JSON column type mapping.</returns>
public static ConfigurationSource? GetJsonColumnTypeMappingConfigurationSource(this IConventionEntityType entityType)
=> entityType.FindAnnotation(RelationalAnnotationNames.JsonColumnTypeMapping)
?.GetConfigurationSource();

/// <summary>
/// Gets the JSON column type mapping to which the entity type is mapped.
/// </summary>
/// <param name="entityType">The entity type to get the JSON column type mapping for.</param>
/// <returns>The JSON column type mapping to which the entity type is mapped.</returns>
public static RelationalTypeMapping? GetJsonColumnTypeMapping(this IReadOnlyEntityType entityType)
=> entityType.FindAnnotation(RelationalAnnotationNames.JsonColumnTypeMapping)?.Value is RelationalTypeMapping jsonColumnTypeMapping
? jsonColumnTypeMapping
: (entityType.FindOwnership()?.PrincipalEntityType.GetJsonColumnTypeMapping());

#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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;

/// <summary>
/// Relational database specific extension methods for <see cref="NavigationBuilder" />.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples.
/// </remarks>
public static class RelationalNavigationBuilderExtensions
{
/// <summary>
/// Configures the navigation of an entity mapped to a JSON column, mapping the navigation to a specific JSON property,
/// rather than using the navigation name.
/// </summary>
/// <param name="navigationBuilder">The builder for the navigation being configured.</param>
/// <param name="name">JSON property name to be used.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>
/// The same builder instance if the configuration was applied,
/// <see langword="null" /> otherwise.
/// </returns>
public static IConventionNavigationBuilder? HasJsonPropertyName(
this IConventionNavigationBuilder navigationBuilder,
string? name,
bool fromDataAnnotation = false)
{
if (!navigationBuilder.CanSetJsonPropertyName(name, fromDataAnnotation))
{
return null;
}

navigationBuilder.Metadata.SetJsonPropertyName(name, fromDataAnnotation);

return navigationBuilder;
}

/// <summary>
/// Returns a value indicating whether the given value can be used as a JSON property name for a given navigation.
/// </summary>
/// <param name="navigationBuilder">The builder for the navigation being configured.</param>
/// <param name="name">JSON property name to be used.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns><see langword="true" /> if the given value can be set as JSON property name for this navigation.</returns>
public static bool CanSetJsonPropertyName(
this IConventionNavigationBuilder navigationBuilder,
string? name,
bool fromDataAnnotation = false)
=> navigationBuilder.CanSetAnnotation(RelationalAnnotationNames.JsonPropertyName, name, fromDataAnnotation);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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;

/// <summary>
/// Navigation extension methods for relational database metadata.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples.
/// </remarks>
public static class RelationalNavigationExtensions
{
/// <summary>
/// Gets the value of JSON property name used for the given navigation of an entity mapped to a JSON column.
/// </summary>
/// <remarks>
/// Unless configured explicitly, navigation name is used.
/// </remarks>
/// <param name="navigation">The navigation.</param>
/// <returns>
/// The value for the JSON property used to store the value of this navigation.
/// <see langword="null" /> is returned for navigations of entities that are not mapped to a JSON column.
/// </returns>
public static string? GetJsonPropertyName(this IReadOnlyNavigationBase navigation)
=> (string?)navigation.FindAnnotation(RelationalAnnotationNames.JsonPropertyName)?.Value
?? (!navigation.DeclaringEntityType.IsMappedToJson() ? null : navigation.Name);

/// <summary>
/// Sets the value of JSON property name used for the given navigation of an entity mapped to a JSON column.
/// </summary>
/// <param name="navigation">The navigation.</param>
/// <param name="name">The name to be used.</param>
public static void SetJsonPropertyName(this IMutableNavigationBase navigation, string? name)
=> navigation.SetOrRemoveAnnotation(
RelationalAnnotationNames.JsonPropertyName,
Check.NullButNotEmpty(name, nameof(name)));

/// <summary>
/// Sets the value of JSON property name used for the given navigation of an entity mapped to a JSON column.
/// </summary>
/// <param name="navigation">The navigation.</param>
/// <param name="name">The name to be used.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>The configured value.</returns>
public static string? SetJsonPropertyName(
this IConventionNavigationBase navigation,
string? name,
bool fromDataAnnotation = false)
{
navigation.SetOrRemoveAnnotation(
RelationalAnnotationNames.JsonPropertyName,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation);

return name;
}

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the JSON property name for a given navigation.
/// </summary>
/// <param name="navigation">The navigation.</param>
/// <returns>The <see cref="ConfigurationSource" /> for the JSON property name for a given navigation.</returns>
public static ConfigurationSource? GetJsonPropertyNameConfigurationSource(this IConventionNavigationBase navigation)
=> navigation.FindAnnotation(RelationalAnnotationNames.JsonPropertyName)?.GetConfigurationSource();
}
Loading