Skip to content

Commit

Permalink
Add ToTable overloads with TableBuilder for OwnedNavigationBuilder
Browse files Browse the repository at this point in the history
Deprecate the excludedFromMigrations overloads
Remove the overloads that take Action<TableBuilder> instead of Action<TableBuilder<TEntity>>
Rename OwnedNavigationBuilder<TEntity, TRelatedEntity> to OwnedNavigationBuilder<TOwnerEntity, TRelatedEntity>
Change TemporalTableBuilder to take IMutableEntityType instead of EntityTypeBuilder
Generate snapshots that call the new overloads.
Fix indentation for nested lambdas in snapshots

Fixes #23173
  • Loading branch information
AndriySvyryd committed Aug 5, 2021
1 parent 5237c80 commit 0c7001d
Show file tree
Hide file tree
Showing 20 changed files with 502 additions and 384 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.Azure.Cosmos;
Expand Down Expand Up @@ -115,10 +114,10 @@ public static OwnedNavigationBuilder ToJsonProperty(
/// <param name="entityTypeBuilder"> The builder for the entity type being configured. </param>
/// <param name="name"> The name of the parent property. </param>
/// <returns> The same builder instance so that multiple calls can be chained. </returns>
public static OwnedNavigationBuilder<TEntity, TDependentEntity> ToJsonProperty<TEntity, TDependentEntity>(
this OwnedNavigationBuilder<TEntity, TDependentEntity> entityTypeBuilder,
public static OwnedNavigationBuilder<TOwnerEntity, TDependentEntity> ToJsonProperty<TOwnerEntity, TDependentEntity>(
this OwnedNavigationBuilder<TOwnerEntity, TDependentEntity> entityTypeBuilder,
string? name)
where TEntity : class
where TOwnerEntity : class
where TDependentEntity : class
{
entityTypeBuilder.OwnedEntityType.SetContainingPropertyName(name);
Expand Down
30 changes: 21 additions & 9 deletions src/EFCore.Design/Design/Internal/CSharpHelper.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Numerics;
using System.Text;
Expand Down Expand Up @@ -137,7 +134,7 @@ public CSharpHelper(ITypeMappingSource typeMappingSource)
{ typeof(Guid), (c, v) => c.Literal((Guid)v) },
{ typeof(int), (c, v) => c.Literal((int)v) },
{ typeof(long), (c, v) => c.Literal((long)v) },
{ typeof(NestedClosureCodeFragment), (c, v) => c.Fragment((NestedClosureCodeFragment)v) },
{ typeof(NestedClosureCodeFragment), (c, v) => c.Fragment((NestedClosureCodeFragment)v, 0) },
{ typeof(object[]), (c, v) => c.Literal((object[])v) },
{ typeof(object[,]), (c, v) => c.Literal((object[,])v) },
{ typeof(sbyte), (c, v) => c.Literal((sbyte)v) },
Expand Down Expand Up @@ -971,8 +968,11 @@ private bool HandleList(IEnumerable<Expression> argumentExpressions, StringBuild
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string Fragment(MethodCallCodeFragment fragment)
=> Fragment(fragment, indent: 0);

private string Fragment(MethodCallCodeFragment fragment, int indent)
{
var builder = new StringBuilder();
var builder = new IndentedStringBuilder();

var current = fragment;
while (current != null)
Expand All @@ -989,7 +989,15 @@ public virtual string Fragment(MethodCallCodeFragment fragment)
builder.Append(", ");
}

builder.Append(UnknownLiteral(current.Arguments[i]));
var argument = current.Arguments[i];
if (argument is NestedClosureCodeFragment nestedFragment)
{
builder.Append(Fragment(nestedFragment, indent));
}
else
{
builder.Append(UnknownLiteral(argument));
}
}

builder.Append(')');
Expand All @@ -1000,21 +1008,25 @@ public virtual string Fragment(MethodCallCodeFragment fragment)
return builder.ToString();
}

private string Fragment(NestedClosureCodeFragment fragment)
private string Fragment(NestedClosureCodeFragment fragment, int indent)
{
if (fragment.MethodCalls.Count == 1)
{
return fragment.Parameter + " => " + fragment.Parameter + Fragment(fragment.MethodCalls[0]);
return fragment.Parameter + " => " + fragment.Parameter + Fragment(fragment.MethodCalls[0], indent);
}

var builder = new IndentedStringBuilder();
builder.AppendLine(fragment.Parameter + " =>");
for (var i = -1; i < indent; i++)
{
builder.IncrementIndent();
}
builder.AppendLine("{");
using (builder.Indent())
{
foreach (var methodCall in fragment.MethodCalls)
{
builder.Append(fragment.Parameter + Fragment(methodCall));
builder.Append(fragment.Parameter + Fragment(methodCall, indent + 1));
builder.AppendLine(";");
}
}
Expand Down
23 changes: 6 additions & 17 deletions src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,10 +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 System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
Expand Down Expand Up @@ -835,7 +832,7 @@ protected virtual void GenerateEntityTypeAnnotations(
.Append(".ToTable(");

if (tableName == null
&& schemaAnnotation == null)
&& (schemaAnnotation == null || schemaAnnotation.Value == null))
{
stringBuilder.Append("(string)");
}
Expand All @@ -848,7 +845,8 @@ protected virtual void GenerateEntityTypeAnnotations(
}

var isExcludedAnnotation = annotations.Find(RelationalAnnotationNames.IsTableExcludedFromMigrations);
if (schemaAnnotation != null)
if (schemaAnnotation != null
&& (tableName != null || schemaAnnotation.Value != null))
{
stringBuilder
.Append(", ");
Expand All @@ -866,17 +864,8 @@ protected virtual void GenerateEntityTypeAnnotations(
{
if (((bool?)isExcludedAnnotation.Value) == true)
{
if (entityType.IsOwned())
{
// Issue #23173
stringBuilder
.Append(", excludedFromMigrations: true");
}
else
{
stringBuilder
.Append(", t => t.ExcludeFromMigrations()");
}
stringBuilder
.Append(", t => t.ExcludeFromMigrations()");
}

annotations.Remove(isExcludedAnnotation.Name);
Expand Down Expand Up @@ -1041,7 +1030,7 @@ protected virtual void GenerateEntityTypeAnnotations(
{
stringBuilder
.AppendLine()
.Append(Code.Fragment(methodCallCodeFragment));
.AppendLines(Code.Fragment(methodCallCodeFragment), skipFinalNewline: true);
}

GenerateAnnotations(annotations.Values, stringBuilder);
Expand Down
Loading

0 comments on commit 0c7001d

Please sign in to comment.