Public/New-ModelProject.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<#
.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
    }

}