Apply migrations only in development #6815
-
Hi, In the following documentation it says Apply migrations at runtime is inappropriate, and it gives a list of the reasons. And all the examples I've seen in .NET Aspire use a worker as a service to run migrations. So how can I run that migration service only in development? var builder = DistributedApplication.CreateBuilder(args);
var postgres = builder.AddPostgres("postgres");
var postgresdb = postgres.AddDatabase("postgresdb");
// How can I run migration only in development?
// if (development) {
var migration = builder.AddProject<Projects.AspireApp_Migration>("migration")
.WithReference(postgresdb)
.WaitFor(postgresdb);
builder.AddProject<Projects.AspireApp_ApiService>("apiservice")
.WithReference(postgresdb)
.WaitForCompletion(migration); I would like to know if is possible to add more information on how to use .NET Aspire + EF Core + Migrations in production. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
See https://github.com/davidfowl/TodoApp/blob/main/TodoApp.AppHost/Program.cs |
Beta Was this translation helpful? Give feedback.
-
@raky291 , exactly what the David put above, it is a great example; thanks for sharing that :) @raky291 , you can use isRunMode and/or IsPublishMode for understanding and creating conditionals for your purpose. An example for executing it only in dev/localhost/run mode: var postgres = builder.AddPostgres("postgres");
var postgresdb = postgres.AddDatabase("postgresdb");
IResourceBuilder migration = null; // Initialize as null
// Conditionally set up the migration only in development mode
if (builder.ExecutionContext.IsRunMode)
{
migration = builder.AddProject<Projects.AspireApp_Migration>("migration")
.WithReference(postgresdb)
.WaitFor(postgresdb);
}
builder.AddProject<Projects.AspireApp_ApiService>("apiservice")
.WithReference(postgresdb)
.WaitForCompletion(migration); // WaitForCompletion handles null gracefully You can understand it better by the docs link below: Regarding how to handle migrations in production, I think it is more related to the company guidelines and policies than Aspire or tooling itself. I hope it helps. |
Beta Was this translation helpful? Give feedback.
@raky291 , exactly what the David put above, it is a great example; thanks for sharing that :)
I will just reinforce:
@raky291 , you can use isRunMode and/or IsPublishMode for understanding and creating conditionals for your purpose.
An example for executing it only in dev/localhost/run mode: