Tuesday, November 8, 2016

Octopus update database by entityframework

Entity framework includes a tool called migrate.exe for use in deployment target to update database.
We can use that one in Octopus deployment. Here is the steps to get it working,

1. create a nuspec file in the project, make it the same name as the project name.
    make sure the id is the same package id as the Octopus package id
Add the below section in the file,
<files>
    <file src="..\packages\EntityFramework.6.1.3\tools\migrate.exe" target="tools"/>
    <file src="..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll" target="tools"/>
    <file src="..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.sqlserver.dll" target="tools"/>
    <file src="*.ps1" />
  </files>

2. Include the parameter OctoPackEnforceAddingFiles for uploading package
msbuild /t:build /p:RunOctoPack=true  /p:OctoPackEnforceAddingFiles=true .....

3. Create a new deployment step script

$installPath = $OctopusParameters['Octopus.Action[Deploy Package].Output.Package.InstallationDirectoryPath']
Write-Host "Installation Path:" $installPath

set-location "$($installPath)\tools"
Write-Host "Working Dir: "$(get-location)

.\migrate.exe <datamodel.dll> /StartUpDirectory="$($installPath)\bin" /StartUpConfigurationFile="$($installPath)\web.config"

No comments: