Public/New-ModelProject.ps1

<#
.SYNOPSIS
    .
.DESCRIPTION

  Make a new C# project to store Entity Framework Core data model.
  This will be a class library with the model and nothing else.
  It will run the dotnet ef scaffolding to create the model from a database connection
  in "database first" style.
.PARAMETER name
    .
.EXAMPLE
    .
.NOTES
    Author: Brian Woelfel
    Date: 2017/09/07
#>

Function New-ModelProject() {
    param (
        [string]$name = "", 
        [string]$dbServer= "", 
        [string]$dbName = "",
        [string[]]$view
    )
    [SolnInfo]$solnInfo = [SolnInfo]::Load()
    
    if($solnInfo.modelCsprojInfo -ne $null) {
        # Proj already exists, but redo the scaffolding
        Write-Host "### Model project already exists so just redo data model scaffolding"
        Remove-Item "$($solnInfo.modelCsprojInfo.csprojDir)\Model\*"
        Remove-Item "$($solnInfo.modelCsprojInfo.csprojDir)\ModelWithView\*"
        New-EfModel $solnInfo $solnInfo.modelCsprojInfo $solnInfo.dbInfo $view
    } else {
        if($name -eq "") {
            $name = "$($solnInfo.nickName)Model"
        }
        if($dbServer -eq "") {
            $dbServer = "localhost"
        }
        if($dbName -eq "") {
            $dbServer = "$($solnInfo.nickName)Dev"
        }
        Write-Host "### Making data model and connection string for dbServer=$dbServer, dbName=$dbName"
        $solnInfo.modelCsprojInfo = [ModelCsprojInfo]::new($solnInfo, $name)
        $solnInfo.dbInfo = [DbInfo]::new($solnInfo, $dbServer, $dbName)
        $solnInfo.SaveConf()

        Write-Host "### Making EntityFramework Core project" $solnInfo.modelCsprojInfo.csprojName
        &{dotnet new classlib -f netcoreapp2.0 -n $solnInfo.modelCsprojInfo.csprojName -o $solnInfo.modelCsprojInfo.csprojDir}
        Confirm-LastExitCode

        # Add proj to soln
        Write-Host "### Add " $solnInfo.modelCsprojInfo.csprojName " to solution " $solnInfo.nickName
        &{dotnet sln $solnInfo.solnFile add $solnInfo.modelCsprojInfo.csprojFile}
        Confirm-LastExitCode

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

        # Add the ability to run "dotnet ef" from the commandline to this project so we can scaffold model
        # It needs to know about a special tool.
        Write-Host "### Upgrading" $solnInfo.modelCsprojInfo.csprojName "to support entity framework scaffolding tool"
        $csprojXml = [xml] (type $solnInfo.modelCsprojInfo.csprojFile)
        [xml]$efXml = 
            @"
<ItemGroup>
  <DotNetCliToolReference
      Include="Microsoft.EntityFrameworkCore.Tools.DotNet"
      Version="2.0.0" />
</ItemGroup>
"@

        $csprojXml.project.AppendChild($csprojXml.ImportNode($efXml.ItemGroup, $true))
        $csprojXml.Save($solnInfo.modelCsprojInfo.csprojFile)

        # Load all the tools and libraries
        Write-Host "### Load all libraries for $($solnInfo.modelCsprojInfo.csprojName)"
        &{dotnet restore $solnInfo.modelCsprojInfo.csprojFile}
        Confirm-LastExitCode

        # Fix startup
        Edit-WebProjectAddDb $solnInfo $solnInfo.webCsprojInfo $solnInfo.modelCsprojInfo $solnInfo.dbInfo

        New-EfModel $solnInfo $solnInfo.modelCsprojInfo $solnInfo.dbInfo $view
    }

}