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

Move table configuration methods to (OwnedNavigation)TableBuilder #28630

Merged
1 commit merged into from
Aug 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public override ConventionSet CreateConventionSet()
conventionSet.Add(new ContextContainerConvention(Dependencies));
conventionSet.Add(new ETagPropertyConvention());
conventionSet.Add(new StoreKeyConvention(Dependencies));

conventionSet.Replace<ValueGenerationConvention>(new CosmosValueGenerationConvention(Dependencies));
conventionSet.Replace<KeyDiscoveryConvention>(new CosmosKeyDiscoveryConvention(Dependencies));
conventionSet.Replace<InversePropertyAttributeConvention>(new CosmosInversePropertyAttributeConvention(Dependencies));
Expand Down
24 changes: 19 additions & 5 deletions src/EFCore.Design/Extensions/ScaffoldingModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ public static IEnumerable<AttributeCodeFragment> GetDataAnnotations(this ISkipNa
/// <param name="entityType">The entity type.</param>
/// <param name="annotationCodeGenerator">The provider's annotation code generator.</param>
/// <returns>The fluent API calls.</returns>
public static FluentApiCodeFragment? GetFluentApiCalls(this IEntityType entityType, IAnnotationCodeGenerator annotationCodeGenerator)
public static FluentApiCodeFragment? GetFluentApiCalls(
this IEntityType entityType, IAnnotationCodeGenerator annotationCodeGenerator)
{
FluentApiCodeFragment? root = null;

Expand All @@ -346,10 +347,11 @@ public static IEnumerable<AttributeCodeFragment> GetDataAnnotations(this ISkipNa

annotations.Remove(RelationalAnnotationNames.TableName);
annotations.Remove(RelationalAnnotationNames.Schema);
annotations.Remove(RelationalAnnotationNames.Comment);
annotations.Remove(RelationalAnnotationNames.ViewName);
annotations.Remove(RelationalAnnotationNames.ViewSchema);
annotations.Remove(ScaffoldingAnnotationNames.DbSetName);
annotations.Remove(RelationalAnnotationNames.ViewDefinitionSql);
annotations.Remove(ScaffoldingAnnotationNames.DbSetName);

var annotationsHandledByDataAnnotations = new Dictionary<string, IAnnotation>(annotations);

Expand Down Expand Up @@ -394,22 +396,34 @@ public static IEnumerable<AttributeCodeFragment> GetDataAnnotations(this ISkipNa
}
}

var toTableNestedCalls = new List<MethodCallCodeFragment>();

var comment = entityType.GetComment();
if (comment != null)
{
toTableHandledByConventions = false;
toTableHandledByDataAnnotations = false;

toTableNestedCalls.Add(new MethodCallCodeFragment(nameof(TableBuilder.HasComment), comment));
}

if (entityType.GetTriggers().Any())
{
toTableHandledByConventions = false;
toTableHandledByDataAnnotations = false;

var toTableNestedCalls = new List<MethodCallCodeFragment>();
foreach (var trigger in entityType.GetTriggers())
{
toTableNestedCalls.Add(new MethodCallCodeFragment(nameof(TableBuilder.HasTrigger), trigger.Name));
}

toTableArguments.Add(new NestedClosureCodeFragment("tb", toTableNestedCalls));
}

if (!toTableHandledByConventions)
{
if (toTableNestedCalls.Count != 0)
{
toTableArguments.Add(new NestedClosureCodeFragment("tb", toTableNestedCalls));
}
var toTable = new FluentApiCodeFragment(nameof(RelationalEntityTypeBuilderExtensions.ToTable))
{
Arguments = toTableArguments,
Expand Down
140 changes: 74 additions & 66 deletions src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ protected virtual void GenerateEntityType(

GenerateEntityTypeAnnotations(entityTypeBuilderName, entityType, stringBuilder);

GenerateCheckConstraints(entityTypeBuilderName, entityType, stringBuilder);

if (ownerNavigation != null)
{
GenerateRelationships(entityTypeBuilderName, entityType, stringBuilder);
Expand Down Expand Up @@ -847,12 +845,32 @@ private void GenerateTableMapping(
Dictionary<string, IAnnotation> annotations)
{
annotations.TryGetAndRemove(RelationalAnnotationNames.TableName, out IAnnotation tableNameAnnotation);
annotations.TryGetAndRemove(RelationalAnnotationNames.Schema, out IAnnotation schemaAnnotation);
var table = StoreObjectIdentifier.Create(entityType, StoreObjectType.Table);
var tableName = (string?)tableNameAnnotation?.Value ?? table?.Name;
if (tableNameAnnotation == null
&& entityType.BaseType != null
&& entityType.BaseType.GetTableName() == tableName)
var explicitName = tableNameAnnotation != null
|| entityType.BaseType == null
|| entityType.BaseType.GetTableName() != tableName;

annotations.TryGetAndRemove(RelationalAnnotationNames.Schema, out IAnnotation schemaAnnotation);
var schema = (string?)schemaAnnotation?.Value ?? table?.Schema;

annotations.TryGetAndRemove(RelationalAnnotationNames.IsTableExcludedFromMigrations, out IAnnotation isExcludedAnnotation);
var isExcludedFromMigrations = (isExcludedAnnotation?.Value as bool?) == true;

annotations.TryGetAndRemove(RelationalAnnotationNames.Comment, out IAnnotation commentAnnotation);
var comment = (string?)commentAnnotation?.Value;

var hasTriggers = entityType.GetTriggers().Any(t => t.TableName == tableName! && t.TableSchema == schema);
var hasOverrides = table != null
&& entityType.GetProperties().Select(p => p.FindOverrides(table.Value)).Any(o => o != null);
var requiresTableBuilder = isExcludedFromMigrations
|| comment != null
|| hasTriggers
|| hasOverrides
|| entityType.GetCheckConstraints().Any();

if (!explicitName
&& !requiresTableBuilder)
{
return;
}
Expand All @@ -861,51 +879,50 @@ private void GenerateTableMapping(
.AppendLine()
.Append(entityTypeBuilderName)
.Append(".ToTable(");

var schema = (string?)schemaAnnotation?.Value ?? table?.Schema;
if (tableName == null
&& (schemaAnnotation == null || schema == null))

if (explicitName)
{
stringBuilder.Append("(string)");
}
if (tableName == null
&& (schemaAnnotation == null || schema == null))
{
stringBuilder.Append("(string)");
}

stringBuilder.Append(Code.Literal(tableName));
stringBuilder.Append(Code.Literal(tableName));

annotations.TryGetAndRemove(RelationalAnnotationNames.IsTableExcludedFromMigrations, out IAnnotation isExcludedAnnotation);
var isExcludedFromMigrations = (isExcludedAnnotation?.Value as bool?) == true;
if (isExcludedAnnotation is not null)
{
annotations.Remove(isExcludedAnnotation.Name);
}

var hasTriggers = entityType.GetTriggers().Any(t => t.TableName == tableName! && t.TableSchema == schema);
var hasOverrides = table != null
&& entityType.GetProperties().Select(p => p.FindOverrides(table.Value)).Any(o => o != null);
var requiresTableBuilder = isExcludedFromMigrations
|| hasTriggers
|| hasOverrides;
if (isExcludedAnnotation is not null)
{
annotations.Remove(isExcludedAnnotation.Name);
}

if (schema != null
|| (schemaAnnotation != null && tableName != null))
{
stringBuilder
.Append(", ");

if (schema == null && !requiresTableBuilder)
if (schema != null
|| (schemaAnnotation != null && tableName != null))
{
stringBuilder.Append("(string)");
}
stringBuilder
.Append(", ");

stringBuilder.Append(Code.Literal(schema));
if (schema == null && !requiresTableBuilder)
{
stringBuilder.Append("(string)");
}

stringBuilder.Append(Code.Literal(schema));
}
}

if (requiresTableBuilder)
{
using (stringBuilder.Indent())
{
if (explicitName)
{
stringBuilder.Append(", ");
}

stringBuilder
.AppendLine(", t =>")
.Append("{");
.AppendLine("t =>")
.Append("{");

using (stringBuilder.Indent())
{
Expand All @@ -915,12 +932,21 @@ private void GenerateTableMapping(
.AppendLine()
.AppendLine("t.ExcludeFromMigrations();");
}

if (comment != null)
{
stringBuilder
.AppendLine()
.AppendLine($"t.{nameof(TableBuilder.HasComment)}({Code.Literal(comment!)});");
}

if (hasTriggers)
{
GenerateTriggers("t", entityType, tableName!, schema, stringBuilder);
}

GenerateCheckConstraints("t", entityType, stringBuilder);

if (hasOverrides)
{
GeneratePropertyOverrides("t", entityType, table!.Value, stringBuilder);
Expand Down Expand Up @@ -1123,10 +1149,10 @@ protected virtual void GenerateCheckConstraint(
.Append(".HasCheckConstraint(")
.Append(Code.Literal(checkConstraint.ModelName))
.Append(", ")
.Append(Code.Literal(checkConstraint.Sql));
.Append(Code.Literal(checkConstraint.Sql))
.Append(")");

GenerateCheckConstraintAnnotations(checkConstraint, stringBuilder);
stringBuilder.AppendLine(");");
}

/// <summary>
Expand All @@ -1143,43 +1169,25 @@ protected virtual void GenerateCheckConstraintAnnotations(
var annotations = Dependencies.AnnotationCodeGenerator
.FilterIgnoredAnnotations(checkConstraint.GetAnnotations())
.ToDictionary(a => a.Name, a => a);
if (annotations.Count > 0
|| hasNonDefaultName)
using (stringBuilder.Indent())
{
if (annotations.Count > 0)
{
stringBuilder
.Append(", c =>")
.AppendLine()
.IncrementIndent()
.AppendLine("{")
.IncrementIndent();
}
else
{
stringBuilder.Append(", c => ");
}

if (hasNonDefaultName)
{
stringBuilder
.Append("c.HasName(")
.AppendLine()
.Append(".HasName(")
.Append(Code.Literal(checkConstraint.Name!))
.Append(")");
}

if (annotations.Count > 0)
{
if (hasNonDefaultName)
{
stringBuilder.AppendLine(";");
}

GenerateAnnotations("c", checkConstraint, stringBuilder, annotations, inChainedCall: false);
stringBuilder
.DecrementIndent()
.Append("}")
.DecrementIndent();
GenerateAnnotations("t", checkConstraint, stringBuilder, annotations, inChainedCall: true);
stringBuilder.IncrementIndent();
}
else
{
stringBuilder.AppendLine(";");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,13 @@ protected virtual ModelBuilder VisitTables(ModelBuilder modelBuilder, ICollectio
}
else
{
builder.ToTable(table.Name, table.Schema);
}

if (table.Comment != null)
{
builder.HasComment(table.Comment);
builder.ToTable(table.Name, table.Schema, tb =>
{
if (table.Comment != null)
{
tb.HasComment(table.Comment);
}
});
}

VisitColumns(builder, table.Columns);
Expand Down
4 changes: 0 additions & 4 deletions src/EFCore.Relational/Design/AnnotationCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,6 @@ public virtual IReadOnlyList<MethodCallCodeFragment> GenerateFluentApiCalls(
{
var methodCallCodeFragments = new List<MethodCallCodeFragment>();

GenerateSimpleFluentApiCall(
annotations,
RelationalAnnotationNames.Comment, nameof(RelationalEntityTypeBuilderExtensions.HasComment), methodCallCodeFragments);

if (annotations.TryGetValue(RelationalAnnotationNames.MappingStrategy, out var mappingStrategyAnnotation)
&& mappingStrategyAnnotation.Value is string mappingStrategy)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ private void Create(IStoredProcedure storedProcedure, string sprocVariable, CSha
.Append(parameters.TargetName).AppendLine(",")
.Append(code.Literal(storedProcedure.Name)).AppendLine(",")
.Append(code.Literal(storedProcedure.Schema)).AppendLine(",")
.Append(code.Literal(storedProcedure.AreRowsAffectedReturned)).AppendLine(",")
.Append(code.Literal(storedProcedure.IsRowsAffectedReturned)).AppendLine(",")
.Append(code.Literal(storedProcedure.AreTransactionsSuppressed))
.AppendLine(");")
.DecrementIndent()
Expand Down
Loading