Private/Cs/WebProj/Edit-WebProject.ps1

<############################################################################
 # Given a Visual Studio ASP DotNet Core Angular website, add an existing
 # Entity Framework project and additional database support code.
 #
 ############################################################################>

Function Edit-WebProjectAddDb([SolnInfo] $solInfo, [WebCsprojInfo] $webCsprojInfo, [CsprojInfo] $modelCsprojInfo, [DbInfo] $dbInfo) 
{
    Write-Host "### Update web project with model"
    Edit-ProjectAddReferencePrivate $webCsprojInfo.csprojFile `
        $modelCsprojInfo.csprojFile

    Write-Host "### Add database nuget packages to" $webCsprojInfo.csprojName
    &{dotnet add $webCsprojInfo.csprojFile package Microsoft.EntityFrameworkCore.SqlServer }
    Confirm-LastExitCode

    # Get the Startup.cs file, add code to read in the connection string
    [string]$startupCsFile = "$($webCsprojInfo.csprojDir)\Startup.cs"
    
    # Tell Startup.cs to initialize database connection from
    # connection string in app*Settings.json

    Write-Host "### Update web Startup.cs with 'using' statements for ef"
    Edit-CsAddUsing $startupCsFile "Microsoft.EntityFrameworkCore"
    Write-Host "### Update web Startup.cs with 'using' statements for $($solnInfo.nickName) Model"
    Edit-CsAddUsing $startupCsFile "$($modelCsprojInfo.namespace).Model"
    Edit-CsAddUsing $startupCsFile "$($modelCsprojInfo.namespace).ModelWithView"

    Write-Host "### Update web Startup.cs to configure entity framework with connection string from appSettings*.json"
    AppendCodeAfterTwoMatchingLines $startupCsFile "public void ConfigureServices" "{" @"
            // Set up EF from connection string
            string connection = Configuration.GetSection("ConnectionStrings").GetValue<string>("$($dbInfo.db)");
            services.AddDbContext <$($modelCsprojInfo.contextName)> (options => options.UseSqlServer(connection));
"@
 "services.AddDbContext"

    Write-Host "### Add connection string to appSettings.Development.json"
    [string]$appSettingsDevelopmentJsonFile = "$($webCsprojInfo.csprojDir)\appSettings.Development.json"
    Edit-WebProjectAddConnStr $appSettingsDevelopmentJsonFile $dbInfo.db $dbInfo.connStr

    # Note for MICROSOFT style, dotnet code generator does standard MVC stuff to wire up pages, but for ANGULAR_IO
    # we have to tell Web API how to serve index.html
    if($webCsprojInfo.angularStyle -eq 'ANGULAR_IO') {
        Write-Host "### Update web Startup.cs to serve index.html"
        AppendCodeAfterTwoMatchingLines $startupCsFile "public void Configure\(IApplicationBuilder app, IHostingEnvironment env\)" "{" @"
                app.Use(async (context, next) =>
                {
                    await next();
                    if (context.Response.StatusCode == 404 &&
                       !System.IO.Path.HasExtension(context.Request.Path.Value) &&
                       !context.Request.Path.Value.StartsWith("/api/"))
                    {
                        context.Request.Path = "/index.html";
                        await next();
                    }
                });
                app.UseMvcWithDefaultRoute();
                app.UseDefaultFiles();
                app.UseStaticFiles();
"@
 "context.Request.Path = "
    }




}

<############################################################################
 # Add database connection string to supplied appSettings.Development.json file name
 # in the correct JSON syntax
 ############################################################################>

Function Edit-WebProjectAddConnStr([string] $jsonFile, [string] $db, [string] $connStr) {
    [string]$fileContents = (Get-Content -raw $jsonFile)
    if(-not ($fileContents -match "ConnectionStrings")) {
        $fileAsJson = (Get-Content -raw $jsonFile | ConvertFrom-Json)
        [string]$newValueAsStr = @"
{
    "$db": "$connStr"
}
"@

        $newValueAsJson = ConvertFrom-Json $newValueAsStr
        $fileAsJson | add-Member -Name "ConnectionStrings" -value $newValueAsJson -MemberType NoteProperty
        $fileAsJson | ConvertTo-Json | Out-FileUtf8NoBom $jsonFile
    }
}