Skip to content

Commit

Permalink
Add a scaffold tool for easily debugging the scaffolder
Browse files Browse the repository at this point in the history
The `dotnet-ef` and `ef` projects go through many hoops (`dotnet exec`) to run the scaffolder (`dotnet ef dbcontext scaffold`)
This makes it almost impossible to set a breakpoint and debug the code. This tools has the minimum code required to run the scaffolder and is easily debuggable with Visual Studio or Rider.
This tool was created so that I could investigate dotnet#25729
  • Loading branch information
0xced committed Aug 27, 2021
1 parent 99a25a6 commit ffbf6fb
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
7 changes: 7 additions & 0 deletions All.sln
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore.AspNet.Sqlite.Functi
EndProject
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}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "scaffold", "src\scaffold\scaffold.csproj", "{906757C0-168D-415A-8103-5E9B1D480F8C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -318,6 +320,10 @@ Global
{F1B2E5A0-8C74-414A-B262-353FEE325E9F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F1B2E5A0-8C74-414A-B262-353FEE325E9F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F1B2E5A0-8C74-414A-B262-353FEE325E9F}.Release|Any CPU.Build.0 = Release|Any CPU
{906757C0-168D-415A-8103-5E9B1D480F8C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{906757C0-168D-415A-8103-5E9B1D480F8C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{906757C0-168D-415A-8103-5E9B1D480F8C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{906757C0-168D-415A-8103-5E9B1D480F8C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -371,6 +377,7 @@ Global
{F956A344-5C8D-4015-A3BF-7A8304C58BE4} = {258D5057-81B9-40EC-A872-D21E27452749}
{CC93C465-F5AC-4CB9-A064-3675955962F4} = {258D5057-81B9-40EC-A872-D21E27452749}
{F1B2E5A0-8C74-414A-B262-353FEE325E9F} = {258D5057-81B9-40EC-A872-D21E27452749}
{906757C0-168D-415A-8103-5E9B1D480F8C} = {CE6B50B2-34AE-44C9-940A-4E48C3E1B3BC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {285A5EB4-BCF4-40EB-B9E1-DF6DBCB5E705}
Expand Down
71 changes: 71 additions & 0 deletions src/scaffold/scaffold.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// The `dotnet-ef` and `ef` projects go through many hoops (`dotnet exec`) to run the scaffolder (`dotnet ef dbcontext scaffold`)
// This makes it almost impossible to set a breakpoint and debug the code. This tools has the minimum code required to run the
// scaffolder and is easily debuggable with Visual Studio or Rider.
// This tool was created so that I could investigate https://github.com/dotnet/efcore/issues/25729

using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Design.Internal;

try
{
if (args.Length is not (1 or 2))
{
Console.Error.WriteLine($"Usage: {Path.GetFileName(Environment.ProcessPath)} <connectionString> [provider]");
return 1;
}

var connectionString = args[0];
var provider = args.Length > 1 ? args[1] : "Microsoft.EntityFrameworkCore.SqlServer";

var operationReportHandler = new OperationReportHandler(
errorHandler: s => Console.WriteLine($@"🟥 {s}"),
warningHandler: s => Console.WriteLine($@"🟧 {s}"),
informationHandler: s => Console.WriteLine($@"🟦️ {s}"),
verboseHandler: s => Console.WriteLine($@"⬜️ {s}")
);
Assembly assembly = typeof(int).Assembly; // System.Private.CoreLib should never have an IDesignTimeServices implementation
var operations = new DatabaseOperations(new OperationReporter(operationReportHandler),
assembly: assembly,
startupAssembly: assembly,
projectDir: ".",
rootNamespace: null,
language: null,
nullable: true,
args: null
);
var savedModelFiles = operations.ScaffoldContext(
provider: provider,
connectionString: connectionString,
outputDir: "generated",
outputContextDir: null,
dbContextClassName: null,
schemas: Enumerable.Empty<string>(),
tables: Enumerable.Empty<string>(),
modelNamespace: null,
contextNamespace: null,
useDataAnnotations: false,
overwriteFiles: true,
useDatabaseNames: false,
suppressOnConfiguring: true,
noPluralize: false
);
Console.WriteLine(@"ℹ️ The following files were saved:");
Console.WriteLine(@"📄 " + savedModelFiles.ContextFile);
foreach (var additionalFile in savedModelFiles.AdditionalFiles)
{
Console.WriteLine(@"📄 " + additionalFile);
}
return 0;
}
catch (Exception exception)
{
Console.Error.WriteLine(@"💥 " + exception);
return 2;
}
15 changes: 15 additions & 0 deletions src/scaffold/scaffold.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\EFCore.Design\EFCore.Design.csproj" />
<ProjectReference Include="..\EFCore.SqlServer\EFCore.SqlServer.csproj" />
</ItemGroup>

</Project>

0 comments on commit ffbf6fb

Please sign in to comment.