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 3, 2022
1 parent aaefc98 commit 6b3bf97
Show file tree
Hide file tree
Showing 53 changed files with 3,598 additions and 127 deletions.
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);
}
}
87 changes: 84 additions & 3 deletions src/EFCore.Relational/Extensions/RelationalEntityTypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -62,7 +63,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 +317,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 +1386,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 +1856,84 @@ public static IEnumerable<ITrigger> GetDeclaredTriggers(this IEntityType entityT
=> Trigger.GetDeclaredTriggers(entityType).Cast<ITrigger>();

#endregion Trigger

/// <summary>
/// TODO
/// </summary>
public static bool IsMappedToJson(this IReadOnlyEntityType entityType)
=> !string.IsNullOrEmpty(entityType.JsonColumnName());

//=> !string.IsNullOrEmpty(entityType.FindAnnotation(RelationalAnnotationNames.JsonColumnName)?.Value as string);

/// <summary>
/// TODO
/// </summary>
public static void SetJsonColumnName(this IMutableEntityType entityType, string? columnName)
=> entityType.SetOrRemoveAnnotation(RelationalAnnotationNames.JsonColumnName, columnName);

/// <summary>
/// TODO
/// </summary>
public static string? SetJsonColumnName(
this IConventionEntityType entityType,
string? columnName,
bool fromDataAnnotation = false)
=> (string?)entityType.SetAnnotation(RelationalAnnotationNames.JsonColumnName, columnName, fromDataAnnotation)?.Value;

/// <summary>
/// TODO
/// </summary>
public static ConfigurationSource? GetJsonColumnNameConfigurationSource(this IConventionEntityType entityType)
=> entityType.FindAnnotation(RelationalAnnotationNames.JsonColumnName)
?.GetConfigurationSource();

/// <summary>
/// TODO
/// </summary>
public static string? JsonColumnName(this IReadOnlyEntityType entityType)
{
var jsonColumnName = entityType.FindAnnotation(RelationalAnnotationNames.JsonColumnName)?.Value as string;
if (jsonColumnName != null)
{
return jsonColumnName;
}

return entityType.FindOwnership()?.PrincipalEntityType.JsonColumnName();
}

/// <summary>
/// TODO
/// </summary>
public static void SetJsonColumnTypeMapping(this IMutableEntityType entityType, RelationalTypeMapping typeMapping)
=> entityType.SetOrRemoveAnnotation(RelationalAnnotationNames.JsonColumnTypeMapping, typeMapping);

/// <summary>
/// TODO
/// </summary>
public static RelationalTypeMapping? SetJsonColumnTypeMapping(
this IConventionEntityType entityType,
RelationalTypeMapping typeMapping,
bool fromDataAnnotation = false)
=> (RelationalTypeMapping?)entityType.SetAnnotation(RelationalAnnotationNames.JsonColumnTypeMapping, typeMapping, fromDataAnnotation)?.Value;

/// <summary>
/// TODO
/// </summary>
public static ConfigurationSource? GetJsonColumnTypeMappingConfigurationSource(this IConventionEntityType entityType)
=> entityType.FindAnnotation(RelationalAnnotationNames.JsonColumnTypeMapping)
?.GetConfigurationSource();

/// <summary>
/// TODO
/// </summary>
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();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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="OwnedNavigationBuilder" />.
/// </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 RelationalOwnedNavigationBuilderExtensions
{
/// <summary>
/// TODO
/// </summary>
public static OwnedNavigationBuilder ToJson(this OwnedNavigationBuilder builder)
{
var navigationName = builder.Metadata.GetNavigation(pointsToPrincipal: false)!.Name;
builder.ToJson(navigationName);

return builder;
}

/// <summary>
/// TODO
/// </summary>
public static OwnedNavigationBuilder<TOwnerEntity, TDependentEntity> ToJson<TOwnerEntity, TDependentEntity>(
this OwnedNavigationBuilder<TOwnerEntity, TDependentEntity> builder)
where TOwnerEntity : class
where TDependentEntity : class
{
var navigationName = builder.Metadata.GetNavigation(pointsToPrincipal: false)!.Name;
builder.ToJson(navigationName);

return builder;
}

/// <summary>
/// TODO
/// </summary>
public static OwnedNavigationBuilder<TOwnerEntity, TDependentEntity> ToJson<TOwnerEntity, TDependentEntity>(
this OwnedNavigationBuilder<TOwnerEntity, TDependentEntity> builder,
string? jsonColumnName)
where TOwnerEntity : class
where TDependentEntity : class
{
builder.OwnedEntityType.SetJsonColumnName(jsonColumnName);

return builder;
}

/// <summary>
/// TODO
/// </summary>
public static OwnedNavigationBuilder ToJson(
this OwnedNavigationBuilder builder,
string? jsonColumnName)
{
builder.OwnedEntityType.SetJsonColumnName(jsonColumnName);

return builder;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1035,4 +1035,53 @@ public static bool CanSetCollation(
string? collation,
bool fromDataAnnotation = false)
=> propertyBuilder.CanSetAnnotation(RelationalAnnotationNames.Collation, collation, fromDataAnnotation);

/// <summary>
/// TODO
/// </summary>
public static PropertyBuilder HasJsonPropertyName(
this PropertyBuilder propertyBuilder,
string? name)
{
Check.NullButNotEmpty(name, nameof(name));

propertyBuilder.Metadata.SetJsonPropertyName(name);

return propertyBuilder;
}

/// <summary>
/// TODO
/// </summary>
public static PropertyBuilder<TProperty> HasJsonPropertyName<TProperty>(
this PropertyBuilder<TProperty> propertyBuilder,
string? name)
=> (PropertyBuilder<TProperty>)HasJsonPropertyName((PropertyBuilder)propertyBuilder, name);

/// <summary>
/// TODO
/// </summary>
public static IConventionPropertyBuilder? HasJsonPropertyName(
this IConventionPropertyBuilder propertyBuilder,
string? name,
bool fromDataAnnotation = false)
{
if (!propertyBuilder.CanSetJsonPropertyName(name, fromDataAnnotation))
{
return null;
}

propertyBuilder.Metadata.SetJsonPropertyName(name, fromDataAnnotation);

return propertyBuilder;
}

/// <summary>
/// TODO
/// </summary>
public static bool CanSetJsonPropertyName(
this IConventionPropertyBuilder propertyBuilder,
string? name,
bool fromDataAnnotation = false)
=> propertyBuilder.CanSetAnnotation(RelationalAnnotationNames.JsonPropertyName, name, fromDataAnnotation);
}
Loading

0 comments on commit 6b3bf97

Please sign in to comment.