Skip to content

Commit

Permalink
Metadata work for json. Separated from main PR to make the review eas…
Browse files Browse the repository at this point in the history
…ier.

This PR includes:

- builder methods,
- conventions,
- relational model,
- model validation,
- migrations,
- update
  • Loading branch information
maumar committed Aug 4, 2022
1 parent aaefc98 commit 1162406
Show file tree
Hide file tree
Showing 54 changed files with 3,925 additions and 127 deletions.
17 changes: 17 additions & 0 deletions src/EFCore.Relational/Design/AnnotationCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,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 @@ -304,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,26 @@
// 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.Extensions.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 IsOrdinalKey(this IReadOnlyProperty property)
=> property.IsKey()
&& property.Name == "Id"
&& property.IsShadowProperty()
&& property.ClrType == typeof(int);
}
}
106 changes: 103 additions & 3 deletions src/EFCore.Relational/Extensions/RelationalEntityTypeExtensions.cs
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,104 @@ public static IEnumerable<ITrigger> GetDeclaredTriggers(this IEntityType entityT
=> Trigger.GetDeclaredTriggers(entityType).Cast<ITrigger>();

#endregion Trigger

/// <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)
=> !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)
{
var jsonColumnName = entityType.FindAnnotation(RelationalAnnotationNames.JsonColumnName)?.Value as string;
if (jsonColumnName != null)
{
return jsonColumnName;
}

return 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)
=> 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)
{
var jsonColumnTypeMapping = entityType.FindAnnotation(RelationalAnnotationNames.JsonColumnTypeMapping)?.Value as RelationalTypeMapping;
if (jsonColumnTypeMapping != null)
{
return jsonColumnTypeMapping;
}

return entityType.FindOwnership()?.PrincipalEntityType.GetJsonColumnTypeMapping();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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>
/// TODO
/// </summary>
public static NavigationBuilder HasJsonPropertyName(
this NavigationBuilder navigationBuilder,
string? name)
{
Check.NullButNotEmpty(name, nameof(name));

navigationBuilder.Metadata.SetJsonPropertyName(name);

return navigationBuilder;
}

/// <summary>
/// TODO
/// </summary>
public static NavigationBuilder<TSource, TTarget> HasJsonPropertyName<TSource, TTarget>(
this NavigationBuilder<TSource, TTarget> navigationBuilder,
string? name)
where TSource : class
where TTarget : class
=> (NavigationBuilder<TSource, TTarget>)HasJsonPropertyName((NavigationBuilder)navigationBuilder, name);

/// <summary>
/// TODO
/// </summary>
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>
/// TODO
/// </summary>
public static bool CanSetJsonPropertyName(
this IConventionNavigationBuilder navigationBuilder,
string? name,
bool fromDataAnnotation = false)
=> navigationBuilder.CanSetAnnotation(RelationalAnnotationNames.JsonPropertyName, name, fromDataAnnotation);
}
50 changes: 50 additions & 0 deletions src/EFCore.Relational/Extensions/RelationalNavigationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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>
/// TODO
/// </summary>
public static string? GetJsonPropertyName(this IReadOnlyNavigationBase navigation)
=> (string?)navigation.FindAnnotation(RelationalAnnotationNames.JsonPropertyName)?.Value
?? (!navigation.DeclaringEntityType.IsMappedToJson() ? null : navigation.Name);

/// <summary>
/// TODO
/// </summary>
public static void SetJsonPropertyName(this IMutableNavigationBase navigation, string? name)
=> navigation.SetOrRemoveAnnotation(
RelationalAnnotationNames.JsonPropertyName,
Check.NullButNotEmpty(name, nameof(name)));

/// <summary>
/// TODO
/// </summary>
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>
/// TODO
/// </summary>
public static ConfigurationSource? GetJsonPropertyNameConfigurationSource(this IConventionNavigationBase navigation)
=> navigation.FindAnnotation(RelationalAnnotationNames.JsonPropertyName)?.GetConfigurationSource();
}
Loading

0 comments on commit 1162406

Please sign in to comment.