I am working on an ASP.NET MVC project and trying to do the following steps in one build:
- Generate models from an existing database with Entity Framework scaffolding.
- Generate controllers from the generated models with ASP.NET Code generator.
- Run NSwag to generate swagger.json.
The problem is that every step requires that the project is built before it, so for example if new controllers are generated in a build, NSwag cannot use them as they are just there as source code, not built yet.
How could I run all the 3 steps in one build and get a correctly built result?
These are the build steps I currently have in my csproj file:
<Target Name="ScaffoldModels" AfterTargets="Build">
<Exec Command="Scripts/scaffold_models.bat" />
</Target>
<Target Name="ScaffoldControllers" AfterTargets="ScaffoldModels">
<Exec Command="Scripts/scaffold_controllers.bat" />
</Target>
<Target Name="NSwag" AfterTargets="ScaffoldControllers">
<Copy SourceFiles="@(Reference)" DestinationFolder="$(OutDir)References" />
<Exec Command="$(NSwagExe_Net60) run nswag.json /variables:Configuration=$(Configuration)" />
<RemoveDir Directories="$(OutDir)References" />
</Target>
scaffold_models.bat:
dotnet ef dbcontext scaffold name=MainDatabase Microsoft.EntityFrameworkCore.SqlServer --force --no-build --data-annotations --output-dir Application/Core/Models --context MainContext
scaffold_controllers.bat:
setlocal enabledelayedexpansion
for %%f in (Application/Core/Models/*) do (
set classname=%%~nf
dotnet-aspnet-codegenerator controller --force --restWithNoViews --dataContext Backend.Application.Core.Models.MainContext --controllerName !classname!Controller --model Backend.Application.Core.Models.!classname! --relativeFolderPath Application/Core/Controllers
)
endlocal