Skip to content

Commit

Permalink
Move table configuration methods to (OwnedNavigation)TableBuilder
Browse files Browse the repository at this point in the history
ConventionsBuilder -> ConventionSetBuilder
AreRowsAffectedReturned -> IsRowsAffectedReturned
IStoreStoredProcedureReturn -> IStoreStoredProcedureReturnValue

Fixes #28205
  • Loading branch information
AndriySvyryd committed Aug 9, 2022
1 parent 6ec0da9 commit 61b0a35
Show file tree
Hide file tree
Showing 49 changed files with 671 additions and 407 deletions.
142 changes: 75 additions & 67 deletions src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ public virtual void Generate(string modelBuilderName, IModel model, IndentedStri

GenerateEntityTypeAnnotations(entityTypeBuilderName, entityType, stringBuilder);

GenerateCheckConstraints(entityTypeBuilderName, entityType, stringBuilder);

if (ownerNavigation != null)
{
GenerateRelationships(entityTypeBuilderName, entityType, stringBuilder);
Expand Down Expand Up @@ -847,65 +845,84 @@ protected virtual void GenerateKeyAnnotations(string keyBuilderName, IKey key, I
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;
}

stringBuilder
.AppendLine()
.Append(entityTypeBuilderName)
.Append(".ToTable(");

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

if (explicitName)
{
stringBuilder.Append("(string)");
}

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

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

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 @@ protected virtual void GenerateKeyAnnotations(string keyBuilderName, IKey key, I
.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 @@ private void GenerateViewMapping(string entityTypeBuilderName, IEntityType entit
.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 @@ private void GenerateViewMapping(string entityTypeBuilderName, IEntityType entit
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
41 changes: 34 additions & 7 deletions src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,11 @@ private void GenerateEntityType(IEntityType entityType)

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);

if (_useDataAnnotations)
{
Expand Down Expand Up @@ -427,7 +428,6 @@ private void GenerateEntityType(IEntityType entityType)
}

var triggers = entityType.GetTriggers().ToArray();

if (triggers.Length > 0)
{
using (_builder.Indent())
Expand Down Expand Up @@ -560,15 +560,42 @@ private void GenerateTableName(IEntityType entityType)

var explicitSchema = schema != null && schema != defaultSchema;
var explicitTable = explicitSchema || tableName != null && tableName != entityType.GetDbSetName();
if (explicitTable)
var comment = entityType.GetComment();
var multiLine = comment != null;
if (explicitTable
|| multiLine)
{
var parameterString = _code.Literal(tableName!);
if (explicitSchema)
var parameterString = "";
if (explicitTable)
{
parameterString += ", " + _code.Literal(schema!);
parameterString = _code.Literal(tableName!);
if (explicitSchema)
{
parameterString += ", " + _code.Literal(schema!);
}
}

var lines = new List<string> { $".{nameof(RelationalEntityTypeBuilderExtensions.ToTable)}({parameterString})" };
var lines = new List<string>();
if (!multiLine)
{
lines.Add($".{nameof(RelationalEntityTypeBuilderExtensions.ToTable)}({parameterString})");
}
else
{
if (parameterString != "")
{
parameterString += ", ";
}
lines.Add($".{nameof(RelationalEntityTypeBuilderExtensions.ToTable)}({parameterString}tb =>");
lines.Add("{");

if (comment != null)
{
lines.Add($"tb.{nameof(TableBuilder.HasComment)}({_code.Literal(comment!)});");
}

lines.Add("})");
}

AppendMultiLineFluentApi(entityType, lines);
}
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
8 changes: 0 additions & 8 deletions src/EFCore.Relational/Design/AnnotationCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ public class AnnotationCodeGenerator : IAnnotationCodeGenerator
= typeof(RelationalModelBuilderExtensions).GetRuntimeMethod(
nameof(RelationalModelBuilderExtensions.UseCollation), new[] { typeof(ModelBuilder), typeof(string) })!;

private static readonly MethodInfo EntityTypeHasCommentMethodInfo
= typeof(RelationalEntityTypeBuilderExtensions).GetRuntimeMethod(
nameof(RelationalEntityTypeBuilderExtensions.HasComment), new[] { typeof(EntityTypeBuilder), typeof(string) })!;

private static readonly MethodInfo EntityTypeUseTpcMappingStrategyMethodInfo
= typeof(RelationalEntityTypeBuilderExtensions).GetRuntimeMethod(
nameof(RelationalEntityTypeBuilderExtensions.UseTpcMappingStrategy), new[] { typeof(EntityTypeBuilder) })!;
Expand Down Expand Up @@ -282,10 +278,6 @@ public virtual void RemoveAnnotationsHandledByConventions(IAnnotatable annotatab
{
var methodCallCodeFragments = new List<MethodCallCodeFragment>();

GenerateSimpleFluentApiCall(
annotations,
RelationalAnnotationNames.Comment, EntityTypeHasCommentMethodInfo, 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

0 comments on commit 61b0a35

Please sign in to comment.