Skip to content

Commit

Permalink
Add relational metadata extensions for conventions.
Browse files Browse the repository at this point in the history
Refactor relational metadata extensions to be directly defined on the metadata objects.
Move conflict resolution between store generated configuration to a convention. Explicit conflicting configuration will now result in an exception.
Mark UseSqlServerIdentityColumn() as obsolete in favor of ForSqlServerUseIdentityColumn()

Part of #214
  • Loading branch information
AndriySvyryd committed May 10, 2019
1 parent 97b7479 commit 469177a
Show file tree
Hide file tree
Showing 360 changed files with 10,157 additions and 11,361 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public bool Apply(InternalEntityTypeBuilder entityTypeBuilder, EntityType oldBas

private static void ConfigureDiscriminator(InternalEntityTypeBuilder entityTypeBuilder)
{
var propertyBuilder = entityTypeBuilder.Property("Discriminator", typeof(string), ConfigurationSource.Convention);
var propertyBuilder = entityTypeBuilder.Property(typeof(string), "Discriminator", ConfigurationSource.Convention);
propertyBuilder.IsRequired(true, ConfigurationSource.Convention);
propertyBuilder.AfterSave(PropertySaveBehavior.Throw, ConfigurationSource.Convention);
propertyBuilder.HasValueGenerator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuild
&& entityType.IsDocumentRoot()
&& !entityType.IsKeyless)
{
var idProperty = entityTypeBuilder.Property(IdPropertyName, typeof(string), ConfigurationSource.Convention);
var idProperty = entityTypeBuilder.Property(typeof(string), IdPropertyName, ConfigurationSource.Convention);
idProperty.HasValueGenerator((_, __) => new IdValueGenerator(), ConfigurationSource.Convention);
entityTypeBuilder.HasKey(new[] { idProperty.Metadata }, ConfigurationSource.Convention);

var jObjectProperty = entityTypeBuilder.Property(JObjectPropertyName, typeof(JObject), ConfigurationSource.Convention);
var jObjectProperty = entityTypeBuilder.Property(typeof(JObject), JObjectPropertyName, ConfigurationSource.Convention);
jObjectProperty.Cosmos(ConfigurationSource.Convention).ToProperty("");
jObjectProperty.ValueGenerated(ValueGenerated.OnAddOrUpdate, ConfigurationSource.Convention);
}
Expand All @@ -50,7 +50,7 @@ public InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuild
var jObjectProperty = entityType.FindDeclaredProperty(JObjectPropertyName);
if (jObjectProperty != null)
{
entityType.Builder.RemoveShadowPropertiesIfUnused(new[] { jObjectProperty });
entityType.Builder.RemoveUnusedShadowProperties(new[] { jObjectProperty });
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,37 @@
// 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 JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Metadata.Internal
{
/// <summary>
/// Provides strongly typed access to scaffolding related annotations on an
/// <see cref="IEntityType" /> instance. Instances of this class are typically obtained via the
/// <see cref="ScaffoldingMetadataExtensions.Scaffolding(IEntityType)" /> extension method and it is not designed
/// to be directly constructed in your application code.
/// 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 class ScaffoldingEntityTypeAnnotations : RelationalEntityTypeAnnotations
public static class ScaffoldingEntityTypeAnnotations
{
/// <summary>
/// Initializes a new instance of the <see cref="ScaffoldingEntityTypeAnnotations" /> class.
/// Instances of this class are typically obtained via the
/// <see cref="ScaffoldingMetadataExtensions.Scaffolding(IEntityType)" /> extension method and it is not designed
/// to be directly constructed in your application code.
/// 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>
/// <param name="entity"> The entity type to access annotation on. </param>
public ScaffoldingEntityTypeAnnotations([NotNull] IEntityType entity)
: base(entity)
{
}
public static string GetDbSetName(this IEntityType entityType)
=> (string)entityType[ScaffoldingAnnotationNames.DbSetName]
?? entityType.Name;

/// <summary>
/// Gets or set the name of the <see cref="DbSet{TEntity}" /> property for this entity type.
/// 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 virtual string DbSetName
{
get => (string)Annotations.Metadata[ScaffoldingAnnotationNames.DbSetName]
?? EntityType.Name;

[param: CanBeNull]
set => Annotations.SetAnnotation(
public static void SetDbSetName(this IMutableEntityType entityType, string value) =>
entityType.SetAnnotation(
ScaffoldingAnnotationNames.DbSetName,
Check.NullButNotEmpty(value, nameof(value)));
}
}
}

This file was deleted.

69 changes: 0 additions & 69 deletions src/EFCore.Design/Metadata/Internal/ScaffoldingModelAnnotations.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// 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 JetBrains.Annotations;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Metadata.Internal
{
Expand All @@ -11,20 +12,25 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Internal
/// 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>
// Issue#11266 This type is being used by provider code. Do not break.
public class RelationalIndexBuilderAnnotations : RelationalIndexAnnotations
public static class ScaffoldingModelExtensions
{
/// <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 RelationalIndexBuilderAnnotations(
[NotNull] InternalIndexBuilder internalBuilder,
ConfigurationSource configurationSource)
: base(new RelationalAnnotationsBuilder(internalBuilder, configurationSource))
public static IDictionary<string, string> GetEntityTypeErrors(this IModel model)
{
var errors = (IDictionary<string, string>)model[ScaffoldingAnnotationNames.EntityTypeErrors];
if (errors == null)
{
errors = new Dictionary<string, string>();
(model as IMutableModel)?.SetEntityTypeErrors(errors);
}

return errors;

}

/// <summary>
Expand All @@ -33,23 +39,29 @@ public RelationalIndexBuilderAnnotations(
/// 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 bool HasName([CanBeNull] string value) => SetName(value);
public static void SetEntityTypeErrors(this IMutableModel model, IDictionary<string, string> value) =>
model.SetAnnotation(
ScaffoldingAnnotationNames.EntityTypeErrors,
Check.NotNull(value, nameof(value)));

/// <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 virtual bool CanSetName([CanBeNull] string value)
=> Annotations.CanSetAnnotation(RelationalAnnotationNames.Name, value);
public static string GetDatabaseName(this IModel model)
=> (string)model[ScaffoldingAnnotationNames.DatabaseName];

/// <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 virtual bool HasFilter([CanBeNull] string value) => SetFilter(value);
public static void SetDatabaseName(this IMutableModel model, string value) =>
model.SetAnnotation(
ScaffoldingAnnotationNames.DatabaseName,
Check.NullButNotEmpty(value, nameof(value)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,26 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Internal
/// 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 class ScaffoldingPropertyAnnotations : RelationalPropertyAnnotations
public static class ScaffoldingPropertyExtensions
{
/// <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 ScaffoldingPropertyAnnotations([NotNull] IProperty property)
: base(property)
{
}
public static int GetColumnOrdinal([NotNull] this IProperty property)
=> (int?)property[ScaffoldingAnnotationNames.ColumnOrdinal] ?? -1;

/// <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 virtual int ColumnOrdinal
{
get => (int)(Annotations.Metadata[ScaffoldingAnnotationNames.ColumnOrdinal] ?? -1);
set => Annotations.SetAnnotation(ScaffoldingAnnotationNames.ColumnOrdinal, value);
}
public static void SetColumnOrdinal([NotNull] this IMutableProperty property, int? ordinal)
=> property.SetOrRemoveAnnotation(
ScaffoldingAnnotationNames.ColumnOrdinal,
ordinal);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ protected virtual void Generate([NotNull] CreateCheckConstraintOperation operati
.Append("table: ")
.Append(Code.Literal(operation.Table))
.AppendLine(",")
.Append("constraintSql: ")
.Append(Code.Literal(operation.ConstraintSql))
.Append("sql: ")
.Append(Code.Literal(operation.Sql))
.Append(")");

Annotations(operation.GetAnnotations(), builder);
Expand Down Expand Up @@ -1084,7 +1084,7 @@ protected virtual void Generate([NotNull] CreateTableOperation operation, [NotNu
.Append("table.CheckConstraint(")
.Append(Code.Literal(checkConstraints.Name))
.Append(", ")
.Append(Code.Literal(checkConstraints.ConstraintSql))
.Append(Code.Literal(checkConstraints.Sql))
.Append(")");

using (builder.Indent())
Expand Down
Loading

0 comments on commit 469177a

Please sign in to comment.