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

[preview 5] Add public conventions interfaces for metadata builders #15436

Merged
merged 1 commit into from
Apr 22, 2019
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
1 change: 1 addition & 0 deletions EFCore.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,6 @@ Licensed under the Apache License, Version 2.0. See License.txt in the project r
<s:Boolean x:Key="/Default/UserDictionary/Words/=keyless/@EntryIndexedValue">True</s:Boolean>

<s:Boolean x:Key="/Default/UserDictionary/Words/=navigations/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=requiredness/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=sqlite/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=unignore/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class CosmosAnnotationsBuilder : CosmosAnnotations
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public CosmosAnnotationsBuilder(
[NotNull] InternalMetadataBuilder internalBuilder,
[NotNull] InternalAnnotatableBuilder internalBuilder,
ConfigurationSource configurationSource)
: base(internalBuilder.Metadata)
{
Expand All @@ -44,7 +44,7 @@ public CosmosAnnotationsBuilder(
/// 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 virtual InternalMetadataBuilder MetadataBuilder { get; }
public virtual InternalAnnotatableBuilder MetadataBuilder { get; }

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -55,7 +55,7 @@ public CosmosAnnotationsBuilder(
public override bool SetAnnotation(
string relationalAnnotationName,
object value)
=> MetadataBuilder.HasAnnotation(relationalAnnotationName, value, ConfigurationSource);
=> MetadataBuilder.HasAnnotation(relationalAnnotationName, value, ConfigurationSource) != null;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -75,6 +75,6 @@ public override bool CanSetAnnotation(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override bool RemoveAnnotation(string annotationName)
=> MetadataBuilder.RemoveAnnotation(annotationName, ConfigurationSource);
=> MetadataBuilder.RemoveAnnotation(annotationName, ConfigurationSource) != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public DiscriminatorBuilder(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
[EntityFrameworkInternal]
protected virtual InternalEntityTypeBuilder EntityTypeBuilder => (InternalEntityTypeBuilder)AnnotationsBuilder.MetadataBuilder;
protected virtual InternalEntityTypeBuilder EntityTypeBuilder => (InternalEntityTypeBuilder)AnnotationsBuilder.GetInfrastructure();

/// <summary>
/// Configures the default discriminator value to use.
Expand Down Expand Up @@ -76,7 +76,7 @@ public virtual DiscriminatorBuilder HasValue<TEntity>([CanBeNull] object value)
public virtual DiscriminatorBuilder HasValue([NotNull] Type entityType, [CanBeNull] object value)
{
var entityTypeBuilder = EntityTypeBuilder.ModelBuilder.Entity(
entityType, AnnotationsBuilder.ConfigurationSource, owned: null);
entityType, AnnotationsBuilder.ConfigurationSource, shouldBeOwned: null);

return HasValue(entityTypeBuilder, value);
}
Expand All @@ -90,7 +90,7 @@ public virtual DiscriminatorBuilder HasValue([NotNull] Type entityType, [CanBeNu
public virtual DiscriminatorBuilder HasValue([NotNull] string entityTypeName, [CanBeNull] object value)
{
var entityTypeBuilder = EntityTypeBuilder.ModelBuilder.Entity(
entityTypeName, AnnotationsBuilder.ConfigurationSource, owned: null);
entityTypeName, AnnotationsBuilder.ConfigurationSource, shouldBeOwned: null);

return HasValue(entityTypeBuilder, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Metadata
namespace Microsoft.EntityFrameworkCore.Metadata.Internal
{
/// <summary>
/// <para>
Expand All @@ -17,7 +17,7 @@ namespace Microsoft.EntityFrameworkCore.Metadata
/// not used in application code.
/// </para>
/// </summary>
public class RelationalAnnotationsBuilder : RelationalAnnotations
public class RelationalAnnotationsBuilder : RelationalAnnotations, IInfrastructure<InternalAnnotatableBuilder>
{
/// <summary>
/// <para>
Expand All @@ -31,12 +31,12 @@ public class RelationalAnnotationsBuilder : RelationalAnnotations
/// <param name="internalBuilder"> The internal builder to use. </param>
/// <param name="configurationSource"> The configuration source. </param>
public RelationalAnnotationsBuilder(
[NotNull] InternalMetadataBuilder internalBuilder,
[NotNull] InternalAnnotatableBuilder internalBuilder,
ConfigurationSource configurationSource)
: base(Check.NotNull(internalBuilder, nameof(internalBuilder)).Metadata)

{
MetadataBuilder = internalBuilder;
AnnotatableBuilder = internalBuilder;
ConfigurationSource = configurationSource;
}

Expand All @@ -53,45 +53,48 @@ public RelationalAnnotationsBuilder(

/// <summary>
/// <para>
/// Returns the <see cref="InternalMetadataBuilder"/> being used.
/// Returns the <see cref="InternalAnnotatableBuilder"/> being used.
/// </para>
/// <para>
/// This property is typically used by database providers (and other extensions). It is generally
/// not used in application code.
/// </para>
/// </summary>
public virtual InternalMetadataBuilder MetadataBuilder { get; }
[EntityFrameworkInternal]
protected virtual InternalAnnotatableBuilder AnnotatableBuilder { get; }

/// <summary>
/// Attempts to set an annotation with the given name to the given value and
/// returns whether or not this was successful.
/// </summary>
/// <param name="annotationName"> The name of the annotation to set. </param>
/// <param name="value"> The value to set. </param>
/// <returns><c>True</c> if the annotation was set; <c>false</c> otherwise. </returns>
/// <returns><c>true</c> if the annotation was set; <c>false</c> otherwise. </returns>
public override bool SetAnnotation(
string annotationName,
object value)
=> MetadataBuilder.HasAnnotation(annotationName, value, ConfigurationSource);
=> AnnotatableBuilder.HasAnnotation(annotationName, value, ConfigurationSource) != null;

/// <summary>
/// Checks whether or not the annotation with the given name can be set to the given value.
/// </summary>
/// <param name="annotationName"> The name of the annotation to set. </param>
/// <param name="value"> The value to set. </param>
/// <returns><c>True</c> if the annotation can be set; <c>false</c> otherwise. </returns>
/// <returns><c>true</c> if the annotation can be set; <c>false</c> otherwise. </returns>
public override bool CanSetAnnotation(
string annotationName,
object value)
=> MetadataBuilder.CanSetAnnotation(annotationName, value, ConfigurationSource);
=> AnnotatableBuilder.CanSetAnnotation(annotationName, value, ConfigurationSource);

/// <summary>
/// Attempts to remove an annotation with the given name and
/// returns whether or not this was successful.
/// </summary>
/// <param name="annotationName"> The name of the annotation to remove. </param>
/// <returns><c>True</c> if the annotation was removed; <c>false</c> otherwise. </returns>
/// <returns><c>true</c> if the annotation was removed; <c>false</c> otherwise. </returns>
public override bool RemoveAnnotation(string annotationName)
=> MetadataBuilder.RemoveAnnotation(annotationName, ConfigurationSource);
=> AnnotatableBuilder.RemoveAnnotation(annotationName, ConfigurationSource) != null;

InternalAnnotatableBuilder IInfrastructure<InternalAnnotatableBuilder>.Instance => AnnotatableBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.EntityFrameworkCore.ValueGeneration.Internal;
Expand Down Expand Up @@ -55,7 +56,7 @@ public RelationalEntityTypeBuilderAnnotations(
/// 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>
protected virtual InternalEntityTypeBuilder EntityTypeBuilder => (InternalEntityTypeBuilder)Annotations.MetadataBuilder;
protected virtual InternalEntityTypeBuilder EntityTypeBuilder => (InternalEntityTypeBuilder)Annotations.GetInfrastructure();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
75 changes: 45 additions & 30 deletions src/EFCore/Extensions/ConventionEntityTypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
Expand Down Expand Up @@ -349,6 +348,37 @@ public static IConventionIndex AddIndex(
return entityType.AddIndex(new[] { property }, fromDataAnnotation);
}

/// <summary>
/// <para>
/// Sets the <see cref="PropertyAccessMode" /> to use for properties of all entity types
/// in this model.
/// </para>
/// <para>
/// Note that individual entity types can override this access mode, and individual properties of
/// entity types can override the access mode set on the entity type. The value set here will
/// be used for any property for which no override has been specified.
/// </para>
/// </summary>
/// <param name="entityType"> The entity type to set the access mode for. </param>
/// <param name="propertyAccessMode"> The <see cref="PropertyAccessMode" />, or <c>null</c> to clear the mode set.</param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
public static void SetPropertyAccessMode(
[NotNull] this IConventionEntityType entityType,
PropertyAccessMode? propertyAccessMode,
bool fromDataAnnotation = false)
=> Check.NotNull(entityType, nameof(entityType)).AsEntityType()
.SetPropertyAccessMode(
propertyAccessMode,
fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention);

/// <summary>
/// Returns the configuration source for <see cref="ModelExtensions.GetPropertyAccessMode" />.
/// </summary>
/// <param name="entityType"> The entity type to set the access mode for. </param>
/// <returns> The configuration source for <see cref="ModelExtensions.GetPropertyAccessMode" />. </returns>
public static ConfigurationSource? GetPropertyAccessModeConfigurationSource([NotNull] this IConventionEntityType entityType)
=> entityType.FindAnnotation(CoreAnnotationNames.PropertyAccessMode)?.GetConfigurationSource();

/// <summary>
/// Sets the change tracking strategy to use for this entity type. This strategy indicates how the
/// context detects changes to properties for an instance of the entity type.
Expand All @@ -358,19 +388,12 @@ public static IConventionIndex AddIndex(
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
public static void SetChangeTrackingStrategy(
[NotNull] this IConventionEntityType entityType,
ChangeTrackingStrategy changeTrackingStrategy,
ChangeTrackingStrategy? changeTrackingStrategy,
bool fromDataAnnotation = false)
{
Check.NotNull(entityType, nameof(entityType));

var errorMessage = entityType.CheckChangeTrackingStrategy(changeTrackingStrategy);
if (errorMessage != null)
{
throw new InvalidOperationException(errorMessage);
}

entityType.SetOrRemoveAnnotation(CoreAnnotationNames.ChangeTrackingStrategy, changeTrackingStrategy, fromDataAnnotation);
}
=> Check.NotNull(entityType, nameof(entityType)).AsEntityType()
.SetChangeTrackingStrategy(
changeTrackingStrategy,
fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention);

/// <summary>
/// Returns the configuration source for <see cref="EntityTypeExtensions.GetChangeTrackingStrategy" />.
Expand All @@ -381,7 +404,7 @@ public static void SetChangeTrackingStrategy(
=> entityType.FindAnnotation(CoreAnnotationNames.ChangeTrackingStrategy)?.GetConfigurationSource();

/// <summary>
/// Sets the LINQ expression filter automatically applied to queries for this entity type.
/// Sets the LINQ expression filter automatically applied to queries for this entity type.
/// </summary>
/// <param name="entityType"> The entity type to set the query filter for. </param>
/// <param name="queryFilter"> The LINQ expression filter. </param>
Expand All @@ -390,17 +413,10 @@ public static void SetQueryFilter(
[NotNull] this IConventionEntityType entityType,
[CanBeNull] LambdaExpression queryFilter,
bool fromDataAnnotation = false)
{
Check.NotNull(entityType, nameof(entityType));

var errorMessage = entityType.CheckQueryFilter(queryFilter);
if (errorMessage != null)
{
throw new InvalidOperationException(errorMessage);
}

entityType.SetOrRemoveAnnotation(CoreAnnotationNames.QueryFilter, queryFilter, fromDataAnnotation);
}
=> Check.NotNull(entityType, nameof(entityType)).AsEntityType()
.SetQueryFilter(
queryFilter,
fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention);

/// <summary>
/// Returns the configuration source for <see cref="EntityTypeExtensions.GetQueryFilter" />.
Expand All @@ -420,11 +436,10 @@ public static void SetDefiningQuery(
[NotNull] this IConventionEntityType entityType,
[CanBeNull] LambdaExpression definingQuery,
bool fromDataAnnotation = false)
{
Check.NotNull(entityType, nameof(entityType));

entityType.SetOrRemoveAnnotation(CoreAnnotationNames.DefiningQuery, definingQuery, fromDataAnnotation);
}
=> Check.NotNull(entityType, nameof(entityType)).AsEntityType()
.SetDefiningQuery(
definingQuery,
fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention);

/// <summary>
/// Returns the configuration source for <see cref="EntityTypeExtensions.GetDefiningQuery" />.
Expand Down
Loading