Skip to content

Commit

Permalink
fixup! Scaffolding: Create default templates
Browse files Browse the repository at this point in the history
Finalize template changes
  • Loading branch information
bricelam committed Aug 8, 2022
1 parent f831c63 commit d6b6273
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 102 deletions.
7 changes: 7 additions & 0 deletions src/EFCore.Design/Design/FluentApiCodeFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ public virtual IEnumerable<string> GetRequiredUsings()
yield return current.Namespace;
}

foreach (var argumentNamespace in current.Arguments
.Where(a => a is not null and not NestedClosureCodeFragment and not PropertyAccessorCodeFragment)
.SelectMany(a => a!.GetType().GetNamespaces()))
{
yield return argumentNamespace;
}

current = current.ChainedCall;
}
while (current is not null);
Expand Down
35 changes: 11 additions & 24 deletions src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
// ------------------------------------------------------------------------------
namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal
{
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Scaffolding;
using Microsoft.Extensions.DependencyInjection;
using System;
Expand All @@ -32,8 +30,6 @@ public partial class CSharpDbContextGenerator : CSharpDbContextGeneratorBase
public virtual string TransformText()
{

// TODO: Remove unused directives above

var services = (IServiceProvider)Host;
var providerCode = services.GetRequiredService<IProviderConfigurationCodeGenerator>();
var annotationCodeGenerator = services.GetRequiredService<IAnnotationCodeGenerator>();
Expand All @@ -43,8 +39,7 @@ public virtual string TransformText()
{
"System",
"System.Collections.Generic",
"Microsoft.EntityFrameworkCore",
"Microsoft.EntityFrameworkCore.Metadata" // TODO: Remove until actually needed
"Microsoft.EntityFrameworkCore"
};

if (NamespaceHint != Options.ModelNamespace
Expand Down Expand Up @@ -140,8 +135,7 @@ public virtual string TransformText()

if (anyConfiguration)
{
// TODO: Try with trailing newlines instead
WriteLine();
WriteLine("");
}

var anyEntityTypeConfiguration = false;
Expand Down Expand Up @@ -181,7 +175,7 @@ public virtual string TransformText()

if (anyEntityTypeConfiguration)
{
WriteLine();
WriteLine("");
}

this.Write(" entity");
Expand All @@ -196,7 +190,7 @@ public virtual string TransformText()
{
if (anyEntityTypeConfiguration)
{
WriteLine();
WriteLine("");
}

var indexFluentApiCalls = index.GetFluentApiCalls(annotationCodeGenerator);
Expand All @@ -219,11 +213,9 @@ public virtual string TransformText()
var firstProperty = true;
foreach (var property in entityType.GetProperties())
{
// TODO: Issue to include things handled by conventions
var propertyFluentApiCalls = property.GetFluentApiCalls(annotationCodeGenerator)
?.FilterChain(c => !(Options.UseDataAnnotations && c.HasDataAnnotation)
// TODO: Helper methods?
&& !(c.Method == "IsRequired" && !property.ClrType.IsValueType && Options.UseNullableReferenceTypes));
&& !(c.Method == "IsRequired" && Options.UseNullableReferenceTypes && !property.ClrType.IsValueType));
if (propertyFluentApiCalls == null)
{
continue;
Expand All @@ -233,7 +225,7 @@ public virtual string TransformText()

if (anyEntityTypeConfiguration && firstProperty)
{
WriteLine();
WriteLine("");
}

this.Write(" entity.Property(e => e.");
Expand All @@ -259,7 +251,7 @@ public virtual string TransformText()

if (anyEntityTypeConfiguration)
{
WriteLine();
WriteLine("");
}

this.Write(" entity.HasOne(d => d.");
Expand All @@ -275,12 +267,11 @@ public virtual string TransformText()
anyEntityTypeConfiguration = true;
}

// TODO: This is complex. Can we combine with the entity type code above? Can we move chunks into helper methods?
foreach (var skipNavigation in entityType.GetSkipNavigations().Where(n => n.IsLeftNavigation()))
{
if (anyEntityTypeConfiguration)
{
WriteLine();
WriteLine("");
}

var left = skipNavigation.ForeignKey;
Expand Down Expand Up @@ -396,7 +387,7 @@ public virtual string TransformText()

if (anyConfiguration)
{
WriteLine();
WriteLine("");
}

this.Write(" OnModelCreatingPartial(modelBuilder);\r\n }\r\n\r\n partial void OnModelC" +
Expand All @@ -414,7 +405,7 @@ public virtual string TransformText()

}

WriteLine();
WriteLine("");

GenerationEnvironment.Append(mainEnvironment);

Expand All @@ -436,10 +427,6 @@ public virtual string TransformText()
}
}

void WriteLine()
=> WriteLine("");


private global::Microsoft.EntityFrameworkCore.Metadata.IModel _ModelField;

/// <summary>
Expand Down
35 changes: 11 additions & 24 deletions src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.tt
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@
<#@ parameter name="Model" type="Microsoft.EntityFrameworkCore.Metadata.IModel" #>
<#@ parameter name="Options" type="Microsoft.EntityFrameworkCore.Scaffolding.ModelCodeGenerationOptions" #>
<#@ parameter name="NamespaceHint" type="System.String" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="Microsoft.EntityFrameworkCore" #>
<#@ import namespace="Microsoft.EntityFrameworkCore.Design" #>
<#@ import namespace="Microsoft.EntityFrameworkCore.Metadata" #>
<#@ import namespace="Microsoft.EntityFrameworkCore.Metadata.Conventions" #>
<#@ import namespace="Microsoft.EntityFrameworkCore.Scaffolding" #>
<#@ import namespace="Microsoft.Extensions.DependencyInjection" #>
<#
// TODO: Remove unused directives above

var services = (IServiceProvider)Host;
var providerCode = services.GetRequiredService<IProviderConfigurationCodeGenerator>();
var annotationCodeGenerator = services.GetRequiredService<IAnnotationCodeGenerator>();
Expand All @@ -27,8 +23,7 @@
{
"System",
"System.Collections.Generic",
"Microsoft.EntityFrameworkCore",
"Microsoft.EntityFrameworkCore.Metadata" // TODO: Remove until actually needed
"Microsoft.EntityFrameworkCore"
};

if (NamespaceHint != Options.ModelNamespace
Expand Down Expand Up @@ -114,8 +109,7 @@ public partial class <#= Options.ContextName #> : DbContext

if (anyConfiguration)
{
// TODO: Try with trailing newlines instead
WriteLine();
WriteLine("");
}

var anyEntityTypeConfiguration = false;
Expand Down Expand Up @@ -150,7 +144,7 @@ public partial class <#= Options.ContextName #> : DbContext

if (anyEntityTypeConfiguration)
{
WriteLine();
WriteLine("");
}
#>
entity<#= code.Fragment(entityTypeFluentApiCalls, indent: 4) #>;
Expand All @@ -163,7 +157,7 @@ public partial class <#= Options.ContextName #> : DbContext
{
if (anyEntityTypeConfiguration)
{
WriteLine();
WriteLine("");
}

var indexFluentApiCalls = index.GetFluentApiCalls(annotationCodeGenerator);
Expand All @@ -180,11 +174,9 @@ public partial class <#= Options.ContextName #> : DbContext
var firstProperty = true;
foreach (var property in entityType.GetProperties())
{
// TODO: Issue to include things handled by conventions
var propertyFluentApiCalls = property.GetFluentApiCalls(annotationCodeGenerator)
?.FilterChain(c => !(Options.UseDataAnnotations && c.HasDataAnnotation)
// TODO: Helper methods?
&& !(c.Method == "IsRequired" && !property.ClrType.IsValueType && Options.UseNullableReferenceTypes));
&& !(c.Method == "IsRequired" && Options.UseNullableReferenceTypes && !property.ClrType.IsValueType));
if (propertyFluentApiCalls == null)
{
continue;
Expand All @@ -194,7 +186,7 @@ public partial class <#= Options.ContextName #> : DbContext

if (anyEntityTypeConfiguration && firstProperty)
{
WriteLine();
WriteLine("");
}
#>
entity.Property(e => e.<#= property.Name #>)<#= code.Fragment(propertyFluentApiCalls, indent: 4) #>;
Expand All @@ -216,20 +208,19 @@ public partial class <#= Options.ContextName #> : DbContext

if (anyEntityTypeConfiguration)
{
WriteLine();
WriteLine("");
}
#>
entity.HasOne(d => d.<#= foreignKey.DependentToPrincipal.Name #>).<#= foreignKey.IsUnique ? "WithOne" : "WithMany" #>(p => p.<#= foreignKey.PrincipalToDependent.Name #>)<#= code.Fragment(foreignKeyFluentApiCalls, indent: 4) #>;
<#
anyEntityTypeConfiguration = true;
}

// TODO: This is complex. Can we combine with the entity type code above? Can we move chunks into helper methods?
foreach (var skipNavigation in entityType.GetSkipNavigations().Where(n => n.IsLeftNavigation()))
{
if (anyEntityTypeConfiguration)
{
WriteLine();
WriteLine("");
}

var left = skipNavigation.ForeignKey;
Expand Down Expand Up @@ -317,7 +308,7 @@ public partial class <#= Options.ContextName #> : DbContext

if (anyConfiguration)
{
WriteLine();
WriteLine("");
}
#>
OnModelCreatingPartial(modelBuilder);
Expand All @@ -336,11 +327,7 @@ using <#= ns #>;
<#
}

WriteLine();
WriteLine("");

GenerationEnvironment.Append(mainEnvironment);
#>
<#+
void WriteLine()
=> WriteLine("");
#>
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public virtual string TransformText()
{
if (!firstProperty)
{
WriteLine();
WriteLine("");
}

if (!string.IsNullOrEmpty(property.GetComment()))
Expand All @@ -106,7 +106,7 @@ public virtual string TransformText()
if (Options.UseDataAnnotations)
{
var dataAnnotations = property.GetDataAnnotations(annotationCodeGenerator)
.Where(a => !(a.Type == typeof(RequiredAttribute) && !property.ClrType.IsValueType && Options.UseNullableReferenceTypes));
.Where(a => !(a.Type == typeof(RequiredAttribute) && Options.UseNullableReferenceTypes && !property.ClrType.IsValueType));
foreach (var dataAnnotation in dataAnnotations)
{

Expand All @@ -119,9 +119,8 @@ public virtual string TransformText()

usings.AddRange(code.GetRequiredUsings(property.ClrType));

// TODO: Helper methods?
var needsNullable = property.IsNullable && !property.ClrType.IsValueType && Options.UseNullableReferenceTypes;
var needsInitializer = !property.IsNullable && !property.ClrType.IsValueType && Options.UseNullableReferenceTypes;
var needsNullable = Options.UseNullableReferenceTypes && property.IsNullable && !property.ClrType.IsValueType;
var needsInitializer = Options.UseNullableReferenceTypes && !property.IsNullable && !property.ClrType.IsValueType;

this.Write(" public ");
this.Write(this.ToStringHelper.ToStringWithCulture(code.Reference(property.ClrType)));
Expand All @@ -137,7 +136,7 @@ public virtual string TransformText()

foreach (var navigation in EntityType.GetNavigations())
{
WriteLine();
WriteLine("");

if (Options.UseDataAnnotations)
{
Expand Down Expand Up @@ -166,9 +165,8 @@ public virtual string TransformText()
}
else
{
// TODO: Helper methods?
var needsNullable = !(navigation.ForeignKey.IsRequired && navigation.IsOnDependent) && Options.UseNullableReferenceTypes;
var needsInitializer = navigation.ForeignKey.IsRequired && navigation.IsOnDependent && Options.UseNullableReferenceTypes;
var needsNullable = Options.UseNullableReferenceTypes && !(navigation.ForeignKey.IsRequired && navigation.IsOnDependent);
var needsInitializer = Options.UseNullableReferenceTypes && navigation.ForeignKey.IsRequired && navigation.IsOnDependent;

this.Write(" public virtual ");
this.Write(this.ToStringHelper.ToStringWithCulture(targetType));
Expand All @@ -184,7 +182,7 @@ public virtual string TransformText()

foreach (var skipNavigation in EntityType.GetSkipNavigations())
{
WriteLine();
WriteLine("");

if (Options.UseDataAnnotations)
{
Expand Down Expand Up @@ -222,7 +220,7 @@ public virtual string TransformText()

}

WriteLine();
WriteLine("");

GenerationEnvironment.Append(previousOutput);

Expand All @@ -244,10 +242,6 @@ public virtual string TransformText()
}
}

void WriteLine()
=> WriteLine("");


private global::Microsoft.EntityFrameworkCore.Metadata.IEntityType _EntityTypeField;

/// <summary>
Expand Down
Loading

0 comments on commit d6b6273

Please sign in to comment.