-
-
Notifications
You must be signed in to change notification settings - Fork 316
Adds support for Using EntityTypeConfiguration files. #2582
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
Merged
ErikEJ
merged 7 commits into
ErikEJ:master
from
jwyza-pi:EntityTypeConfiguration-support
Nov 8, 2024
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a8cf5ac
Adds support for Using EntityTypeConfiguration files.
jwyza-pi 9fce422
Fix code smells
jwyza-pi a77b785
Adds new use-t4-split option. Adds example t4's configured for Entit…
jwyza-pi c8e1cb1
Fix version for EntityTypeConfiguraiton to 8.
jwyza-pi ec5e986
Merge branch 'master' into EntityTypeConfiguration-support
jwyza-pi cbdf8a1
Fixes T800 Zip file nesting.
jwyza-pi b0a6ce4
PR Feedback.
jwyza-pi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
270 changes: 270 additions & 0 deletions
270
samples/CodeTemplates/EFCore/EntityTypeConfiguration.t4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,270 @@ | ||
<#@ template hostSpecific="true" debug="false" #> | ||
<#@ assembly name="Microsoft.EntityFrameworkCore" #> | ||
<#@ assembly name="Microsoft.EntityFrameworkCore.Design" #> | ||
<#@ assembly name="Microsoft.EntityFrameworkCore.Relational" #> | ||
<#@ assembly name="Microsoft.Extensions.DependencyInjection.Abstractions" #> | ||
<#@ parameter name="EntityType" type="Microsoft.EntityFrameworkCore.Metadata.IEntityType" #> | ||
<#@ parameter name="Options" type="Microsoft.EntityFrameworkCore.Scaffolding.ModelCodeGenerationOptions" #> | ||
<#@ parameter name="NamespaceHint" type="System.String" #> | ||
<#@ parameter name="ProjectDefaultNamespace" type="System.String" #> | ||
<#@ 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.Infrastructure" #> | ||
<#@ import namespace="Microsoft.EntityFrameworkCore.Scaffolding" #> | ||
<#@ import namespace="Microsoft.Extensions.DependencyInjection" #> | ||
<#@ import namespace="Microsoft.EntityFrameworkCore.Metadata.Builders" #> | ||
<# | ||
if (!ProductInfo.GetVersion().StartsWith("7.0")) | ||
{ | ||
Warning("Your templates were created using an older version of Entity Framework. Additional features and bug fixes may be available. See https://aka.ms/efcore-docs-updating-templates for more information."); | ||
} | ||
|
||
var services = (IServiceProvider)Host; | ||
var providerCode = services.GetRequiredService<IProviderConfigurationCodeGenerator>(); | ||
var annotationCodeGenerator = services.GetRequiredService<IAnnotationCodeGenerator>(); | ||
var code = services.GetRequiredService<ICSharpHelper>(); | ||
|
||
var usings = new List<string> | ||
{ | ||
"System", | ||
"System.Collections.Generic", | ||
"Microsoft.EntityFrameworkCore" | ||
}; | ||
|
||
if (NamespaceHint != Options.ModelNamespace | ||
&& !string.IsNullOrEmpty(Options.ModelNamespace)) | ||
{ | ||
usings.Add(Options.ModelNamespace); | ||
} | ||
usings.Add(typeof(EntityTypeBuilder<>).Namespace); | ||
|
||
if (!string.IsNullOrEmpty(NamespaceHint)) | ||
{ | ||
#> | ||
namespace <#= NamespaceHint #>.Configurations; | ||
|
||
<# | ||
} | ||
#> | ||
public partial class <#= EntityType.Name #>Configuration : IEntityTypeConfiguration<<#= EntityType.Name #>> | ||
{ | ||
public void Configure(EntityTypeBuilder<<#= EntityType.Name #>> entity) | ||
{ | ||
<# | ||
var anyConfiguration = false; | ||
|
||
StringBuilder mainEnvironment; | ||
if (EntityType?.Name!=null) | ||
{ | ||
// Save all previously generated code, and start generating into a new temporary environment | ||
mainEnvironment = GenerationEnvironment; | ||
GenerationEnvironment = new StringBuilder(); | ||
|
||
var anyEntityTypeConfiguration = false; | ||
var key = EntityType.FindPrimaryKey(); | ||
if (key != null) | ||
{ | ||
var keyFluentApiCalls = key.GetFluentApiCalls(annotationCodeGenerator); | ||
if (keyFluentApiCalls != null | ||
|| (!key.IsHandledByConvention() && !Options.UseDataAnnotations)) | ||
{ | ||
if (keyFluentApiCalls != null) | ||
{ | ||
usings.AddRange(keyFluentApiCalls.GetRequiredUsings()); | ||
} | ||
#> | ||
entity.HasKey(<#= code.Lambda(key.Properties, "e") #>)<#= code.Fragment(keyFluentApiCalls, indent: 3) #>; | ||
<# | ||
anyEntityTypeConfiguration = true; | ||
} | ||
} | ||
|
||
var entityTypeFluentApiCalls = EntityType.GetFluentApiCalls(annotationCodeGenerator) | ||
?.FilterChain(c => !(Options.UseDataAnnotations && c.IsHandledByDataAnnotations)); | ||
if (entityTypeFluentApiCalls != null) | ||
{ | ||
usings.AddRange(entityTypeFluentApiCalls.GetRequiredUsings()); | ||
|
||
if (anyEntityTypeConfiguration) | ||
{ | ||
WriteLine(""); | ||
} | ||
#> | ||
entity<#= code.Fragment(entityTypeFluentApiCalls, indent: 3) #>; | ||
<# | ||
anyEntityTypeConfiguration = true; | ||
} | ||
|
||
foreach (var index in EntityType.GetIndexes() | ||
.Where(i => !(Options.UseDataAnnotations && i.IsHandledByDataAnnotations(annotationCodeGenerator)))) | ||
{ | ||
if (anyEntityTypeConfiguration) | ||
{ | ||
WriteLine(""); | ||
} | ||
|
||
var indexFluentApiCalls = index.GetFluentApiCalls(annotationCodeGenerator); | ||
if (indexFluentApiCalls != null) | ||
{ | ||
usings.AddRange(indexFluentApiCalls.GetRequiredUsings()); | ||
} | ||
#> | ||
entity.HasIndex(<#= code.Lambda(index.Properties, "e") #>, <#= code.Literal(index.GetDatabaseName()) #>)<#= code.Fragment(indexFluentApiCalls, indent: 3) #>; | ||
<# | ||
anyEntityTypeConfiguration = true; | ||
} | ||
|
||
var firstProperty = true; | ||
foreach (var property in EntityType.GetProperties()) | ||
{ | ||
var propertyFluentApiCalls = property.GetFluentApiCalls(annotationCodeGenerator) | ||
?.FilterChain(c => !(Options.UseDataAnnotations && c.IsHandledByDataAnnotations) | ||
&& !(c.Method == "IsRequired" && Options.UseNullableReferenceTypes && !property.ClrType.IsValueType)); | ||
if (propertyFluentApiCalls == null) | ||
{ | ||
continue; | ||
} | ||
|
||
usings.AddRange(propertyFluentApiCalls.GetRequiredUsings()); | ||
|
||
if (anyEntityTypeConfiguration && firstProperty) | ||
{ | ||
WriteLine(""); | ||
} | ||
#> | ||
entity.Property(e => e.<#= property.Name #>)<#= code.Fragment(propertyFluentApiCalls, indent: 3) #>; | ||
<# | ||
anyEntityTypeConfiguration = true; | ||
firstProperty = false; | ||
} | ||
|
||
foreach (var foreignKey in EntityType.GetForeignKeys()) | ||
{ | ||
var foreignKeyFluentApiCalls = foreignKey.GetFluentApiCalls(annotationCodeGenerator) | ||
?.FilterChain(c => !(Options.UseDataAnnotations && c.IsHandledByDataAnnotations)); | ||
if (foreignKeyFluentApiCalls == null) | ||
{ | ||
continue; | ||
} | ||
|
||
usings.AddRange(foreignKeyFluentApiCalls.GetRequiredUsings()); | ||
|
||
if (anyEntityTypeConfiguration) | ||
{ | ||
WriteLine(""); | ||
} | ||
if (foreignKey.DependentToPrincipal?.Name != null && foreignKey.PrincipalToDependent?.Name != null) | ||
{ | ||
#> | ||
entity.HasOne(d => d.<#= foreignKey.DependentToPrincipal.Name #>).<#= foreignKey.IsUnique ? "WithOne" : "WithMany" #>(p => p.<#= foreignKey.PrincipalToDependent.Name #>)<#= code.Fragment(foreignKeyFluentApiCalls, indent: 3) #>; | ||
<# | ||
} | ||
anyEntityTypeConfiguration = true; | ||
} | ||
|
||
foreach (var skipNavigation in EntityType.GetSkipNavigations().Where(n => n.IsLeftNavigation())) | ||
{ | ||
if (anyEntityTypeConfiguration) | ||
{ | ||
WriteLine(""); | ||
} | ||
|
||
var left = skipNavigation.ForeignKey; | ||
var leftFluentApiCalls = left.GetFluentApiCalls(annotationCodeGenerator, useStrings: true); | ||
var right = skipNavigation.Inverse.ForeignKey; | ||
var rightFluentApiCalls = right.GetFluentApiCalls(annotationCodeGenerator, useStrings: true); | ||
var joinEntityType = skipNavigation.JoinEntityType; | ||
|
||
if (leftFluentApiCalls != null) | ||
{ | ||
usings.AddRange(leftFluentApiCalls.GetRequiredUsings()); | ||
} | ||
|
||
if (rightFluentApiCalls != null) | ||
{ | ||
usings.AddRange(rightFluentApiCalls.GetRequiredUsings()); | ||
} | ||
#> | ||
entity.HasMany(d => d.<#= skipNavigation.Name #>).WithMany(p => p.<#= skipNavigation.Inverse.Name #>) | ||
.UsingEntity<Dictionary<string, object>>( | ||
<#= code.Literal(joinEntityType.Name) #>, | ||
r => r.HasOne<<#= right.PrincipalEntityType.Name #>>().WithMany()<#= code.Fragment(rightFluentApiCalls, indent: 6) #>, | ||
l => l.HasOne<<#= left.PrincipalEntityType.Name #>>().WithMany()<#= code.Fragment(leftFluentApiCalls, indent: 6) #>, | ||
j => | ||
{ | ||
<# | ||
var joinKey = joinEntityType.FindPrimaryKey(); | ||
var joinKeyFluentApiCalls = joinKey.GetFluentApiCalls(annotationCodeGenerator); | ||
|
||
if (joinKeyFluentApiCalls != null) | ||
{ | ||
usings.AddRange(joinKeyFluentApiCalls.GetRequiredUsings()); | ||
} | ||
#> | ||
j.HasKey(<#= code.Arguments(joinKey.Properties.Select(e => e.Name)) #>)<#= code.Fragment(joinKeyFluentApiCalls, indent: 7) #>; | ||
<# | ||
var joinEntityTypeFluentApiCalls = joinEntityType.GetFluentApiCalls(annotationCodeGenerator); | ||
if (joinEntityTypeFluentApiCalls != null) | ||
{ | ||
usings.AddRange(joinEntityTypeFluentApiCalls.GetRequiredUsings()); | ||
#> | ||
j<#= code.Fragment(joinEntityTypeFluentApiCalls, indent: 7) #>; | ||
<# | ||
} | ||
|
||
foreach (var index in joinEntityType.GetIndexes()) | ||
{ | ||
var indexFluentApiCalls = index.GetFluentApiCalls(annotationCodeGenerator); | ||
if (indexFluentApiCalls != null) | ||
{ | ||
usings.AddRange(indexFluentApiCalls.GetRequiredUsings()); | ||
} | ||
#> | ||
j.HasIndex(<#= code.Literal(index.Properties.Select(e => e.Name).ToArray()) #>, <#= code.Literal(index.GetDatabaseName()) #>)<#= code.Fragment(indexFluentApiCalls, indent: 7) #>; | ||
<# | ||
} | ||
#> | ||
}); | ||
<# | ||
anyEntityTypeConfiguration = true; | ||
} | ||
|
||
// If any significant code was generated, append it to the main environment | ||
if (anyEntityTypeConfiguration) | ||
{ | ||
mainEnvironment.Append(GenerationEnvironment); | ||
anyConfiguration = true; | ||
} | ||
|
||
// Resume generating code into the main environment | ||
GenerationEnvironment = mainEnvironment; | ||
} | ||
|
||
if (anyConfiguration) | ||
{ | ||
WriteLine(""); | ||
} | ||
#> | ||
OnConfigurePartial(entity); | ||
} | ||
|
||
partial void OnConfigurePartial(EntityTypeBuilder<<#= EntityType.Name #>> modelBuilder); | ||
} | ||
<# | ||
mainEnvironment = GenerationEnvironment; | ||
GenerationEnvironment = new StringBuilder(); | ||
|
||
foreach (var ns in usings.Distinct().OrderBy(x => x, new NamespaceComparer())) | ||
{ | ||
#> | ||
using <#= ns #>; | ||
<# | ||
} | ||
|
||
WriteLine(""); | ||
|
||
GenerationEnvironment.Append(mainEnvironment); | ||
#> |
jwyza-pi marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.