Skip to content

Commit 7a9756c

Browse files
authored
Add Microsoft.EntityFrameworkCore.Templates (#28678)
Adds a new package that can be installed via `dotnet new`: dotnet new install Microsoft.EntityFrameworkCore.Templates::7.0.0-* For now, it just includes a single item template that will add the default T4 templates used by `dotnet ef dbcontext scaffold`. dotnet new ef-templates You can then tweak the templates and they'll automatically be used when scaffolding. dotnet ef dbcontext scaffold Filename=northwind.db Microsoft.EntityFrameworkCore.Sqlite Resolves #4038
1 parent 5b27554 commit 7a9756c

File tree

4 files changed

+153
-2
lines changed

4 files changed

+153
-2
lines changed

All.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore.AspNet.Sqlite.Functi
121121
EndProject
122122
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore.AspNet.InMemory.FunctionalTests", "test\EFCore.AspNet.InMemory.FunctionalTests\EFCore.AspNet.InMemory.FunctionalTests.csproj", "{F1B2E5A0-8C74-414A-B262-353FEE325E9F}"
123123
EndProject
124-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EFCore.Trimming.Tests", "test\EFCore.Trimming.Tests\EFCore.Trimming.Tests.csproj", "{933C8662-817C-4F45-B98B-6557E28F7BB1}"
124+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore.Trimming.Tests", "test\EFCore.Trimming.Tests\EFCore.Trimming.Tests.csproj", "{933C8662-817C-4F45-B98B-6557E28F7BB1}"
125+
EndProject
126+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore.Templates", "src\EFCore.Templates\EFCore.Templates.csproj", "{1FE385D8-8F8B-4EC9-A1A9-AFCC38B8546C}"
125127
EndProject
126128
Global
127129
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -325,6 +327,10 @@ Global
325327
{933C8662-817C-4F45-B98B-6557E28F7BB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
326328
{933C8662-817C-4F45-B98B-6557E28F7BB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
327329
{933C8662-817C-4F45-B98B-6557E28F7BB1}.Release|Any CPU.Build.0 = Release|Any CPU
330+
{1FE385D8-8F8B-4EC9-A1A9-AFCC38B8546C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
331+
{1FE385D8-8F8B-4EC9-A1A9-AFCC38B8546C}.Debug|Any CPU.Build.0 = Debug|Any CPU
332+
{1FE385D8-8F8B-4EC9-A1A9-AFCC38B8546C}.Release|Any CPU.ActiveCfg = Release|Any CPU
333+
{1FE385D8-8F8B-4EC9-A1A9-AFCC38B8546C}.Release|Any CPU.Build.0 = Release|Any CPU
328334
EndGlobalSection
329335
GlobalSection(SolutionProperties) = preSolution
330336
HideSolutionNode = FALSE
@@ -379,6 +385,7 @@ Global
379385
{CC93C465-F5AC-4CB9-A064-3675955962F4} = {258D5057-81B9-40EC-A872-D21E27452749}
380386
{F1B2E5A0-8C74-414A-B262-353FEE325E9F} = {258D5057-81B9-40EC-A872-D21E27452749}
381387
{933C8662-817C-4F45-B98B-6557E28F7BB1} = {258D5057-81B9-40EC-A872-D21E27452749}
388+
{1FE385D8-8F8B-4EC9-A1A9-AFCC38B8546C} = {CE6B50B2-34AE-44C9-940A-4E48C3E1B3BC}
382389
EndGlobalSection
383390
GlobalSection(ExtensibilityGlobals) = postSolution
384391
SolutionGuid = {285A5EB4-BCF4-40EB-B9E1-DF6DBCB5E705}

EFCore.Tools.slnf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"projects": [
55
"src\\dotnet-ef\\dotnet-ef.csproj",
66
"src\\ef\\ef.csproj",
7-
"src\\EFCore.Design\\EFCore.Design.csproj",
7+
"src\\EFCore.Design\\EFCore.Design.csproj",
8+
"src\\EFCore.Templates\\EFCore.Templates.csproj",
89
"src\\EFCore.Tools\\EFCore.Tools.csproj",
910
"test\\dotnet-ef.Tests\\dotnet-ef.Tests.csproj",
1011
"test\\ef.Tests\\ef.Tests.csproj",
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<PackageType>Template</PackageType>
5+
<PackageId>Microsoft.EntityFrameworkCore.Templates</PackageId>
6+
<Description>
7+
Entity Framework Core templates for dotnet-new.
8+
9+
Enables these commonly used templates:
10+
ef-templates
11+
</Description>
12+
<TargetFramework>netstandard2.0</TargetFramework>
13+
<IncludeContentInPack>true</IncludeContentInPack>
14+
<IncludeBuildOutput>false</IncludeBuildOutput>
15+
<ContentTargetFolders>content</ContentTargetFolders>
16+
<NoWarn>$(NoWarn);NU5128</NoWarn>
17+
<NoDefaultExcludes>true</NoDefaultExcludes>
18+
<IncludeSymbols>false</IncludeSymbols>
19+
</PropertyGroup>
20+
21+
<ItemGroup>
22+
<Content Include="templates\**\*" Exclude="templates\**\bin\**;templates\**\obj\**" />
23+
<Compile Remove="**\*" />
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<GeneratedContent Include="..\EFCore.Design\Scaffolding\Internal\CSharpDbContextGenerator.tt">
28+
<PackagePath>content\templates\ef-templates\CodeTemplates\EFCore\DbContext.t4</PackagePath>
29+
<IsDbContext>true</IsDbContext>
30+
</GeneratedContent>
31+
<GeneratedContent Include="..\EFCore.Design\Scaffolding\Internal\CSharpEntityTypeGenerator.tt">
32+
<PackagePath>content\templates\ef-templates\CodeTemplates\EFCore\EntityType.t4</PackagePath>
33+
</GeneratedContent>
34+
</ItemGroup>
35+
36+
<ItemGroup>
37+
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
38+
</ItemGroup>
39+
40+
<UsingTask TaskName="PatchTemplate" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
41+
<ParameterGroup>
42+
<TemplateFile ParameterType="System.String" Required="true" />
43+
<OutputPath ParameterType="System.String" Required="true" />
44+
<IsDbContext ParameterType="System.Boolean" />
45+
<VersionPrefix ParameterType="System.String" />
46+
</ParameterGroup>
47+
<Task>
48+
<Using Namespace="System.IO" />
49+
<Code Type="Fragment" Language="cs">
50+
<![CDATA[
51+
const string importLine = @"<#@ import namespace=""Microsoft.EntityFrameworkCore.Infrastructure"" #>";
52+
53+
var linePragmasRemoved = false;
54+
var importAdded = !IsDbContext;
55+
var versionCheckAdded = !IsDbContext;
56+
57+
Directory.CreateDirectory(Path.GetDirectoryName(OutputPath));
58+
59+
using (var reader = new StreamReader(TemplateFile))
60+
using (var writer = new StreamWriter(OutputPath))
61+
{
62+
string line;
63+
while ((line = reader.ReadLine()) != null)
64+
{
65+
if (!linePragmasRemoved
66+
&& line.StartsWith("<#@ template "))
67+
{
68+
writer.WriteLine(line.Replace(@" linePragmas=""false""", ""));
69+
70+
linePragmasRemoved = true;
71+
}
72+
else if (!importAdded
73+
&& line.StartsWith("<#@ import ")
74+
&& !line.Contains(@" namespace=""System.")
75+
&& string.Compare(line, importLine) > 0
76+
&& !line.Contains(@" namespace=""Microsoft.EntityFrameworkCore"""))
77+
{
78+
writer.WriteLine(importLine);
79+
writer.WriteLine(line);
80+
81+
importAdded = true;
82+
}
83+
else if (!versionCheckAdded
84+
&& line == "<#")
85+
{
86+
var version = new Version(VersionPrefix);
87+
88+
writer.WriteLine(line);
89+
writer.WriteLine(@" if (!ProductInfo.GetVersion().StartsWith(""" + version.Major + "." + version.Minor + @"""))");
90+
writer.WriteLine(@" {");
91+
writer.WriteLine(@" 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."");");
92+
writer.WriteLine(@" }");
93+
writer.WriteLine();
94+
95+
versionCheckAdded = true;
96+
}
97+
else
98+
{
99+
writer.WriteLine(line);
100+
}
101+
}
102+
}
103+
104+
if (!linePragmasRemoved
105+
|| !importAdded
106+
|| !versionCheckAdded)
107+
{
108+
Log.LogError("Failed to patch the template files. Update the code in EFCore.Templates.csproj");
109+
110+
return false;
111+
}
112+
]]>
113+
</Code>
114+
</Task>
115+
</UsingTask>
116+
117+
<PropertyGroup>
118+
<BeforePack>$(BeforePack);GenerateContent</BeforePack>
119+
</PropertyGroup>
120+
121+
<Target Name="GenerateContent">
122+
<PatchTemplate TemplateFile="%(GeneratedContent.Identity)"
123+
OutputPath="$(IntermediateOutputPath)%(GeneratedContent.PackagePath)"
124+
IsDbContext="%(GeneratedContent.IsDbContext)"
125+
VersionPrefix="$(VersionPrefix)"/>
126+
<ItemGroup>
127+
<Content Include="$(IntermediateOutputPath)%(GeneratedContent.PackagePath)" PackagePath="$([System.IO.Path]::GetDirectoryName(%(GeneratedContent.PackagePath)))" />
128+
</ItemGroup>
129+
</Target>
130+
131+
</Project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"$schema": "http://json.schemastore.org/template",
3+
"author": "Microsoft",
4+
"classifications": [ "EFCore", "Scaffolding" ],
5+
"identity": "Microsoft.EntityFrameworkCore.Templates.ef-templates",
6+
"name": "Entity Framework Core Scaffolding Templates",
7+
"shortName": "ef-templates",
8+
"tags": {
9+
"language": "C#",
10+
"type": "item"
11+
}
12+
}

0 commit comments

Comments
 (0)