0

I can't figure out how to manually update the database (without a workaround) in a .net Aspire app using Entity Framework Core. The issue is similar to this one, but my question is different:

In my AppHost, I spin up a PostgesDB and pass a reference to the project myApp, where I add the DB context builder.AddNpgsqlDbContext<MyDbContext>("myDB"). I can manually apply migrations using dotnet ef migrations add Init --project myApp and updating the data base programmatically as described here kind of works.

When I try manually updating the database with dotnet ef database update --project myApp I get System.ArgumentException: Host can't be null, which is clear, because AppHost is not running, so there is no DB container. I found this workaround works when using the --no-build option (otherwise the app locks some files that need to be updated during build), but it's a workaround.

However, I expected that I could spin up the AppHost by additionally specifying the startup project like dotnet ef database update --project myApp --startup-project AspireDB.AppHost. But now I get Could not load assembly 'myApp'. Ensure it is referenced by the startup project 'AspireDB.AppHost', which puzzles me, as the app works when I run it, so there is a reference to myApp.

Why does --startup-project not work as I expect?

4
  • The startup project you want is the service or web app that contains the DbContext, not the AppHost that coordinates them. EF Core migrations know nothing about Aspire nor are they affected by it. The problem isn't really different. Commented Oct 11, 2024 at 9:02
  • Well, when I choose the web app that contains the DbContext by running dotnet ef ... in the myApp directory, I get "Host cannot be null", because Aspire did not start the container running the DB. So I need to run the AppHost to spin up the container and provide the connnection string. But when I do, I get the error that myApp is not refereenced ?!? Commented Oct 12, 2024 at 17:41
  • Check Apply Entity Framework Core migrations in .NET Aspire. Commented Oct 14, 2024 at 7:10
  • Thanks, this just explains how to programmatically update the DB. As far as I understand, the recommended way to update a production DB is to generate a script from dotnet ef databse update, so I would rather update the DB manually and not programmatically. Commented Oct 15, 2024 at 11:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.