123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777 |
- $InitialDatabase = '0'
- $knownExceptions = @(
- 'System.Data.Entity.Migrations.Infrastructure.MigrationsException',
- 'System.Data.Entity.Migrations.Infrastructure.AutomaticMigrationsDisabledException',
- 'System.Data.Entity.Migrations.Infrastructure.AutomaticDataLossException',
- 'System.Data.Entity.Migrations.MigrationsPendingException',
- 'System.Data.Entity.Migrations.ProjectTypeNotSupportedException'
- )
- function Enable-Migrations
- {
- [CmdletBinding(DefaultParameterSetName = 'ConnectionStringName')]
- param (
- [string] $ContextTypeName,
- [alias('Auto')]
- [switch] $EnableAutomaticMigrations,
- [string] $ProjectName,
- [string] $StartUpProjectName,
- [parameter(ParameterSetName = 'ConnectionStringName')]
- [string] $ConnectionStringName,
- [parameter(ParameterSetName = 'ConnectionStringAndProviderName',
- Mandatory = $true)]
- [string] $ConnectionString,
- [parameter(ParameterSetName = 'ConnectionStringAndProviderName',
- Mandatory = $true)]
- [string] $ConnectionProviderName,
- [switch] $Force
- )
- $runner = New-MigrationsRunner $ProjectName $StartUpProjectName $null $ConnectionStringName $ConnectionString $ConnectionProviderName
- try
- {
- Invoke-RunnerCommand $runner System.Data.Entity.Migrations.EnableMigrationsCommand @( $EnableAutomaticMigrations.IsPresent, $Force.IsPresent ) @{ 'ContextTypeName' = $ContextTypeName }
- $error = Get-RunnerError $runner
-
- if ($error)
- {
- if ($knownExceptions -notcontains $error.TypeName)
- {
- Write-Host $error.StackTrace
- }
- throw $error.Message
- }
- }
- finally
- {
- Remove-Runner $runner
- }
- }
- function Add-Migration
- {
- [CmdletBinding(DefaultParameterSetName = 'ConnectionStringName')]
- param (
- [parameter(Position = 0,
- Mandatory = $true)]
- [string] $Name,
- [switch] $Force,
- [string] $ProjectName,
- [string] $StartUpProjectName,
- [string] $ConfigurationTypeName,
- [parameter(ParameterSetName = 'ConnectionStringName')]
- [string] $ConnectionStringName,
- [parameter(ParameterSetName = 'ConnectionStringAndProviderName',
- Mandatory = $true)]
- [string] $ConnectionString,
- [parameter(ParameterSetName = 'ConnectionStringAndProviderName',
- Mandatory = $true)]
- [string] $ConnectionProviderName,
- [switch] $IgnoreChanges)
- $runner = New-MigrationsRunner $ProjectName $StartUpProjectName $ConfigurationTypeName $ConnectionStringName $ConnectionString $ConnectionProviderName
- try
- {
- Invoke-RunnerCommand $runner System.Data.Entity.Migrations.AddMigrationCommand @( $Name, $Force.IsPresent, $IgnoreChanges.IsPresent )
- $error = Get-RunnerError $runner
-
- if ($error)
- {
- if ($knownExceptions -notcontains $error.TypeName)
- {
- Write-Host $error.StackTrace
- }
- throw $error.Message
- }
- }
- finally
- {
- Remove-Runner $runner
- }
- }
- function Update-Database
- {
- [CmdletBinding(DefaultParameterSetName = 'ConnectionStringName')]
- param (
- [string] $SourceMigration,
- [string] $TargetMigration,
- [switch] $Script,
- [switch] $Force,
- [string] $ProjectName,
- [string] $StartUpProjectName,
- [string] $ConfigurationTypeName,
- [parameter(ParameterSetName = 'ConnectionStringName')]
- [string] $ConnectionStringName,
- [parameter(ParameterSetName = 'ConnectionStringAndProviderName',
- Mandatory = $true)]
- [string] $ConnectionString,
- [parameter(ParameterSetName = 'ConnectionStringAndProviderName',
- Mandatory = $true)]
- [string] $ConnectionProviderName)
- $runner = New-MigrationsRunner $ProjectName $StartUpProjectName $ConfigurationTypeName $ConnectionStringName $ConnectionString $ConnectionProviderName
- try
- {
- Invoke-RunnerCommand $runner System.Data.Entity.Migrations.UpdateDatabaseCommand @( $SourceMigration, $TargetMigration, $Script.IsPresent, $Force.IsPresent, $Verbose.IsPresent )
- $error = Get-RunnerError $runner
-
- if ($error)
- {
- if ($knownExceptions -notcontains $error.TypeName)
- {
- Write-Host $error.StackTrace
- }
- throw $error.Message
- }
- }
- finally
- {
- Remove-Runner $runner
- }
- }
- function Get-Migrations
- {
- [CmdletBinding(DefaultParameterSetName = 'ConnectionStringName')]
- param (
- [string] $ProjectName,
- [string] $StartUpProjectName,
- [string] $ConfigurationTypeName,
- [parameter(ParameterSetName = 'ConnectionStringName')]
- [string] $ConnectionStringName,
- [parameter(ParameterSetName = 'ConnectionStringAndProviderName',
- Mandatory = $true)]
- [string] $ConnectionString,
- [parameter(ParameterSetName = 'ConnectionStringAndProviderName',
- Mandatory = $true)]
- [string] $ConnectionProviderName)
- $runner = New-MigrationsRunner $ProjectName $StartUpProjectName $ConfigurationTypeName $ConnectionStringName $ConnectionString $ConnectionProviderName
- try
- {
- Invoke-RunnerCommand $runner System.Data.Entity.Migrations.GetMigrationsCommand
- $error = Get-RunnerError $runner
-
- if ($error)
- {
- if ($knownExceptions -notcontains $error.TypeName)
- {
- Write-Host $error.StackTrace
- }
- throw $error.Message
- }
- }
- finally
- {
- Remove-Runner $runner
- }
- }
- function New-MigrationsRunner($ProjectName, $StartUpProjectName, $ConfigurationTypeName, $ConnectionStringName, $ConnectionString, $ConnectionProviderName)
- {
- $startUpProject = Get-MigrationsStartUpProject $StartUpProjectName $ProjectName
- Build-Project $startUpProject
- $project = Get-MigrationsProject $ProjectName
- Build-Project $project
- $installPath = Get-EntityFrameworkInstallPath $project
- $toolsPath = Join-Path $installPath tools
- $info = New-Object System.AppDomainSetup -Property @{
- ShadowCopyFiles = 'true';
- ApplicationBase = $installPath;
- PrivateBinPath = 'tools'
- }
- $targetFrameworkVersion = (New-Object System.Runtime.Versioning.FrameworkName ($project.Properties.Item('TargetFrameworkMoniker').Value)).Version
- if ($targetFrameworkVersion -lt (New-Object Version @( 4, 5 )))
- {
- $info.PrivateBinPath += ';lib\net40'
- $dteVersion = [System.Text.RegularExpressions.Regex]::Match($DTE.Version, '^(?<version>\d{1,2}(\.\d{1,2})?)( \(.+\))?$').Groups['version'].Value
- if ((New-Object Version $dteVersion) -lt (New-Object Version @( 11, 0 )))
- {
- $info.ConfigurationFile = Join-Path $toolsPath 'Redirect.config'
- }
- else
- {
- $info.ConfigurationFile = Join-Path $toolsPath 'Redirect.VS11.config'
- }
- }
- else
- {
- $info.PrivateBinPath += ';lib\net45'
- $info.ConfigurationFile = [AppDomain]::CurrentDomain.SetupInformation.ConfigurationFile
- }
- $domain = [AppDomain]::CreateDomain('Migrations', $null, $info)
- $domain.SetData('project', $project)
- $domain.SetData('startUpProject', $startUpProject)
- $domain.SetData('configurationTypeName', $ConfigurationTypeName)
- $domain.SetData('connectionStringName', $ConnectionStringName)
- $domain.SetData('connectionString', $ConnectionString)
- $domain.SetData('connectionProviderName', $ConnectionProviderName)
-
- [AppDomain]::CurrentDomain.SetShadowCopyFiles()
- $utilityAssembly = [System.Reflection.Assembly]::LoadFrom((Join-Path $toolsPath EntityFramework.PowerShell.Utility.dll))
- $dispatcher = $utilityAssembly.CreateInstance(
- 'System.Data.Entity.Migrations.Utilities.DomainDispatcher',
- $false,
- [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::Public,
- $null,
- $PSCmdlet,
- $null,
- $null)
- $domain.SetData('efDispatcher', $dispatcher)
- return @{
- Domain = $domain;
- ToolsPath = $toolsPath
- }
- }
- function Remove-Runner($runner)
- {
- [AppDomain]::Unload($runner.Domain)
- }
- function Invoke-RunnerCommand($runner, $command, $parameters, $anonymousArguments)
- {
- $domain = $runner.Domain
- if ($anonymousArguments)
- {
- $anonymousArguments.GetEnumerator() | %{
- $domain.SetData($_.Name, $_.Value)
- }
- }
- $domain.CreateInstanceFrom(
- (Join-Path $runner.ToolsPath EntityFramework.PowerShell.dll),
- $command,
- $false,
- 0,
- $null,
- $parameters,
- $null,
- $null) | Out-Null
- }
- function Get-RunnerError($runner)
- {
- $domain = $runner.Domain
- if (!$domain.GetData('wasError'))
- {
- return $null
- }
- return @{
- Message = $domain.GetData('error.Message');
- TypeName = $domain.GetData('error.TypeName');
- StackTrace = $domain.GetData('error.StackTrace')
- }
- }
- function Get-MigrationsProject($name, $hideMessage)
- {
- if ($name)
- {
- return Get-SingleProject $name
- }
- $project = Get-Project
- $projectName = $project.Name
- if (!$hideMessage)
- {
- Write-Verbose "Using NuGet project '$projectName'."
- }
- return $project
- }
- function Get-MigrationsStartUpProject($name, $fallbackName)
- {
- $startUpProject = $null
- if ($name)
- {
- $startUpProject = Get-SingleProject $name
- }
- else
- {
- $startupProjectPaths = $DTE.Solution.SolutionBuild.StartupProjects
- if ($startupProjectPaths)
- {
- if ($startupProjectPaths.Length -eq 1)
- {
- $startupProjectPath = $startupProjectPaths[0]
- if (!(Split-Path -IsAbsolute $startupProjectPath))
- {
- $solutionPath = Split-Path $DTE.Solution.Properties.Item('Path').Value
- $startupProjectPath = Join-Path $solutionPath $startupProjectPath -Resolve
- }
- $startupProject = Get-SolutionProjects | ?{
- try
- {
- $fullName = $_.FullName
- }
- catch [NotImplementedException]
- {
- return false;
- }
- if ($fullName -and $fullName.EndsWith('\'))
- {
- $fullName = $fullName.Substring(0, $fullName.Length - 1)
- }
- return $fullName -eq $startupProjectPath
- }
- }
- else
- {
- Write-Verbose 'More than one start-up project found.'
- }
- }
- else
- {
- Write-Verbose 'No start-up project found.'
- }
- }
- if (!($startUpProject -and (Test-StartUpProject $startUpProject)))
- {
- $startUpProject = Get-MigrationsProject $fallbackName $true
- $startUpProjectName = $startUpProject.Name
- Write-Warning "Cannot determine a valid start-up project. Using project '$startUpProjectName' instead. Your configuration file and working directory may not be set as expected. Use the -StartUpProjectName parameter to set one explicitly. Use the -Verbose switch for more information."
- }
- else
- {
- $startUpProjectName = $startUpProject.Name
- Write-Verbose "Using StartUp project '$startUpProjectName'."
- }
- return $startUpProject
- }
- function Get-SolutionProjects()
- {
- $projects = New-Object System.Collections.Stack
-
- $DTE.Solution.Projects | %{
- $projects.Push($_)
- }
-
- while ($projects.Count -ne 0)
- {
- $project = $projects.Pop();
-
-
- $project
- if ($project.ProjectItems)
- {
- $project.ProjectItems | ?{ $_.SubProject } | %{
- $projects.Push($_.SubProject)
- }
- }
- }
- }
- function Get-SingleProject($name)
- {
- $project = Get-Project $name
- if ($project -is [array])
- {
- throw "More than one project '$name' was found. Specify the full name of the one to use."
- }
- return $project
- }
- function Test-StartUpProject($project)
- {
- if ($project.Kind -eq '{cc5fd16d-436d-48ad-a40c-5a424c6e3e79}')
- {
- $projectName = $project.Name
- Write-Verbose "Cannot use start-up project '$projectName'. The Windows Azure Project type isn't supported."
-
- return $false
- }
-
- return $true
- }
- function Build-Project($project)
- {
- $configuration = $DTE.Solution.SolutionBuild.ActiveConfiguration.Name
- $DTE.Solution.SolutionBuild.BuildProject($configuration, $project.UniqueName, $true)
- if ($DTE.Solution.SolutionBuild.LastBuildInfo)
- {
- $projectName = $project.Name
- throw "The project '$projectName' failed to build."
- }
- }
- function Get-EntityFrameworkInstallPath($project)
- {
- $package = Get-Package -ProjectName $project.FullName | ?{ $_.Id -eq 'EntityFramework' }
- if (!$package)
- {
- $projectName = $project.Name
- throw "The EntityFramework package is not installed on project '$projectName'."
- }
-
- return Get-PackageInstallPath $package
- }
-
- function Get-PackageInstallPath($package)
- {
- $componentModel = Get-VsComponentModel
- $packageInstallerServices = $componentModel.GetService([NuGet.VisualStudio.IVsPackageInstallerServices])
- $vsPackage = $packageInstallerServices.GetInstalledPackages() | ?{ $_.Id -eq $package.Id -and $_.Version -eq $package.Version }
-
- return $vsPackage.InstallPath
- }
- Export-ModuleMember @( 'Enable-Migrations', 'Add-Migration', 'Update-Database', 'Get-Migrations' ) -Variable InitialDatabase
|