1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054 |
- $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.Infrastructure.MigrationsPendingException',
- 'System.Data.Entity.Migrations.ProjectTypeNotSupportedException'
- )
- function Add-EFProvider
- {
- param (
- [parameter(Position = 0,
- Mandatory = $true)]
- $Project,
- [parameter(Position = 1,
- Mandatory = $true)]
- [string] $InvariantName,
- [parameter(Position = 2,
- Mandatory = $true)]
- [string] $TypeName
- )
- Check-Project $project
- $runner = New-EFConfigRunner $Project
- try
- {
- Invoke-RunnerCommand $runner System.Data.Entity.ConnectionFactoryConfig.AddProviderCommand @( $InvariantName, $TypeName )
- $error = Get-RunnerError $runner
- if ($error)
- {
- if ($knownExceptions -notcontains $error.TypeName)
- {
- Write-Host $error.StackTrace
- }
- else
- {
- Write-Verbose $error.StackTrace
- }
- throw $error.Message
- }
- }
- finally
- {
- Remove-Runner $runner
- }
- }
- function Add-EFDefaultConnectionFactory
- {
- param (
- [parameter(Position = 0,
- Mandatory = $true)]
- $Project,
- [parameter(Position = 1,
- Mandatory = $true)]
- [string] $TypeName,
- [string[]] $ConstructorArguments
- )
- Check-Project $project
- $runner = New-EFConfigRunner $Project
- try
- {
- Invoke-RunnerCommand $runner System.Data.Entity.ConnectionFactoryConfig.AddDefaultConnectionFactoryCommand @( $TypeName, $ConstructorArguments )
- $error = Get-RunnerError $runner
- if ($error)
- {
- if ($knownExceptions -notcontains $error.TypeName)
- {
- Write-Host $error.StackTrace
- }
- else
- {
- Write-Verbose $error.StackTrace
- }
- throw $error.Message
- }
- }
- finally
- {
- Remove-Runner $runner
- }
- }
- function Initialize-EFConfiguration
- {
- param (
- [parameter(Position = 0,
- Mandatory = $true)]
- $Project
- )
- Check-Project $project
- $runner = New-EFConfigRunner $Project
- try
- {
- Invoke-RunnerCommand $runner System.Data.Entity.ConnectionFactoryConfig.InitializeEntityFrameworkCommand
- $error = Get-RunnerError $runner
- if ($error)
- {
- if ($knownExceptions -notcontains $error.TypeName)
- {
- Write-Host $error.StackTrace
- }
- else
- {
- Write-Verbose $error.StackTrace
- }
- throw $error.Message
- }
- }
- finally
- {
- Remove-Runner $runner
- }
- }
- function Enable-Migrations
- {
- [CmdletBinding(DefaultParameterSetName = 'ConnectionStringName')]
- param (
- [string] $ContextTypeName,
- [alias('Auto')]
- [switch] $EnableAutomaticMigrations,
- [string] $MigrationsDirectory,
- [string] $ProjectName,
- [string] $StartUpProjectName,
- [string] $ContextProjectName,
- [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 $ContextProjectName $null $ConnectionStringName $ConnectionString $ConnectionProviderName
- try
- {
- Invoke-RunnerCommand $runner System.Data.Entity.Migrations.EnableMigrationsCommand @( $EnableAutomaticMigrations.IsPresent, $Force.IsPresent ) @{ 'ContextTypeName' = $ContextTypeName; 'MigrationsDirectory' = $MigrationsDirectory }
- $error = Get-RunnerError $runner
- if ($error)
- {
- if ($knownExceptions -notcontains $error.TypeName)
- {
- Write-Host $error.StackTrace
- }
- else
- {
- Write-Verbose $error.StackTrace
- }
- throw $error.Message
- }
- $(Get-VSComponentModel).GetService([NuGetConsole.IPowerConsoleWindow]).Show()
- }
- 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 $null $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
- }
- else
- {
- Write-Verbose $error.StackTrace
- }
- throw $error.Message
- }
- $(Get-VSComponentModel).GetService([NuGetConsole.IPowerConsoleWindow]).Show()
- }
- 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 $null $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
- }
- else
- {
- Write-Verbose $error.StackTrace
- }
- throw $error.Message
- }
- $(Get-VSComponentModel).GetService([NuGetConsole.IPowerConsoleWindow]).Show()
- }
- 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 $null $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
- }
- else
- {
- Write-Verbose $error.StackTrace
- }
- throw $error.Message
- }
- }
- finally
- {
- Remove-Runner $runner
- }
- }
- function New-MigrationsRunner($ProjectName, $StartUpProjectName, $ContextProjectName, $ConfigurationTypeName, $ConnectionStringName, $ConnectionString, $ConnectionProviderName)
- {
- $startUpProject = Get-MigrationsStartUpProject $StartUpProjectName $ProjectName
- Build-Project $startUpProject
- $project = Get-MigrationsProject $ProjectName
- Build-Project $project
- $contextProject = $project
- if ($ContextProjectName)
- {
- $contextProject = Get-SingleProject $ContextProjectName
- Build-Project $contextProject
- }
- $installPath = Get-EntityFrameworkInstallPath $project
- $toolsPath = Join-Path $installPath tools
- $info = New-AppDomainSetup $project $installPath
- $domain = [AppDomain]::CreateDomain('Migrations', $null, $info)
- $domain.SetData('project', $project)
- $domain.SetData('contextProject', $contextProject)
- $domain.SetData('startUpProject', $startUpProject)
- $domain.SetData('configurationTypeName', $ConfigurationTypeName)
- $domain.SetData('connectionStringName', $ConnectionStringName)
- $domain.SetData('connectionString', $ConnectionString)
- $domain.SetData('connectionProviderName', $ConnectionProviderName)
-
- $dispatcher = New-DomainDispatcher $toolsPath
- $domain.SetData('efDispatcher', $dispatcher)
- return @{
- Domain = $domain;
- ToolsPath = $toolsPath
- }
- }
- function New-EFConfigRunner($Project)
- {
- $installPath = Get-EntityFrameworkInstallPath $Project
- $toolsPath = Join-Path $installPath tools
- $info = New-AppDomainSetup $Project $installPath
- $domain = [AppDomain]::CreateDomain('EFConfig', $null, $info)
- $domain.SetData('project', $Project)
-
- $dispatcher = New-DomainDispatcher $toolsPath
- $domain.SetData('efDispatcher', $dispatcher)
- return @{
- Domain = $domain;
- ToolsPath = $toolsPath
- }
- }
- function New-AppDomainSetup($Project, $InstallPath)
- {
- $info = New-Object System.AppDomainSetup -Property @{
- ShadowCopyFiles = 'true';
- ApplicationBase = $InstallPath;
- PrivateBinPath = 'tools';
- ConfigurationFile = ([AppDomain]::CurrentDomain.SetupInformation.ConfigurationFile)
- }
-
- $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'
- }
- else
- {
- $info.PrivateBinPath += ';lib\net45'
- }
- return $info
- }
- function New-DomainDispatcher($ToolsPath)
- {
- $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)
- return $dispatcher
- }
- 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
- }
- function Check-Project($project)
- {
- if (!$project.FullName)
- {
- throw "The Project argument must refer to a Visual Studio project. Use the '`$project' variable provided by NuGet when running in install.ps1."
- }
- }
- Export-ModuleMember @( 'Enable-Migrations', 'Add-Migration', 'Update-Database', 'Get-Migrations', 'Add-EFProvider', 'Add-EFDefaultConnectionFactory', 'Initialize-EFConfiguration') -Variable InitialDatabase
|