dotnetcore-buildpack: Error while running migrations with "heroku run dotnet ef database update"

I used this buildpack to deploy to a Heroku app, and since it’s a new deployment, I expected that I would need to run all the migrations to get the database ready for the app to run. Since I would run dotnet ef database update locally to do this, I figured I would heroku run dotnet ef database update to run this remotely. I have used Heroku before for Rails and I remember being able to do all the rake tasks by prepending them with heroku run.

When I run this though, it takes a long time to do something with the local package cache and then it displays an error message:

> heroku run dotnet ef database update
Running dotnet ef database update on ⬢ property-app-back-end... up, run.5524 (Free)

Welcome to .NET Core!
---------------------
Learn more about .NET Core @ https://aka.ms/dotnet-docs. Use dotnet --help to see available commands or go to https://aka.ms/dotnet-cli-docs.

Telemetry
--------------
The .NET Core tools collect usage data in order to improve your experience. The data is anonymous and does not include command-line arguments. The data is collected by Microsoft and shared with the community.
You can opt out of telemetry by setting a DOTNET_CLI_TELEMETRY_OPTOUT environment variable to 1 using your favorite shell.
You can read more about .NET Core tools telemetry @ https://aka.ms/dotnet-cli-telemetry.

Configuring...
-------------------
A command is running to initially populate your local package cache, to improve restore speed and enable offline access. This command will take up to a minute to complete and will only happen once.
Decompressing 100% 8288 ms
Expanding 100% 17908 ms
Version for package `Microsoft.EntityFrameworkCore.Tools.DotNet` could not be resolved.

Any idea what might be causing this?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 21 (18 by maintainers)

Commits related to this issue

Most upvoted comments

@luizs81 Thanks for bringing that up. I never considered it when I wrote my comment on Apr 15. I’ve since changed my approach to be more dynamic. I came up with an algorithm to parse the Heroku-style connection URL to a connection string that Npgsql can use. I’m glad you brought it up because anyone following my old comment may have had some issues in the future. 👍

Here is my new approach:

// Heroku provides PostgreSQL connection URL via env variable
var connUrl = Environment.GetEnvironmentVariable("DATABASE_URL");

// Parse connection URL to connection string for Npgsql
connUrl = connUrl.Replace("postgres://", string.Empty);

var pgUserPass = connUrl.Split("@")[0];
var pgHostPortDb = connUrl.Split("@")[1];
var pgHostPort = pgHostPortDb.Split("/")[0];

var pgDb = pgHostPortDb.Split("/")[1];
var pgUser = pgUserPass.Split(":")[0];
var pgPass = pgUserPass.Split(":")[1];
var pgHost = pgHostPort.Split(":")[0];
var pgPort = pgHostPort.Split(":")[1];

connStr = $"Server={pgHost};Port={pgPort};User Id={pgUser};Password={pgPass};Database={pgDb}";

Hi @welkie,

Right, heroku run dotnet ef database update doesn’t work after application has published.

I suggest to add target to csproj file (https://github.com/jincod/AspNetHerokuTest/blob/9a38aae85790d7f11755e41e9844da606981758e/AspNetHerokuTest.csproj#L13-L15):

  <Target Name="PrePublishTarget" AfterTargets="Publish">
    <Exec Command="dotnet ef database update" />
  </Target>

Also I had to add some options to connection string

sslmode=Prefer;Trust Server Certificate=true

https://github.com/jincod/AspNetHerokuTest/commit/9a38aae85790d7f11755e41e9844da606981758e

Source code: https://gitlab.com/mattwelke/AspNetCorePostgresHeroku

Fixed project here https://github.com/jincod/AspNetHerokuTest

Let me know please, if it works for you

@jincod I’ll be testing this buildback again soon with 2.1. I’ll let you know if I experience this issue running migrations on remote again.

No seeding in Startup. All start up time database work done in Program. 😃