microsoft / microsoft/aspire

Not able to create EF migrations when using Aspire environment variables

Open
#8,678 7 comments 0 reactions 0 assignees View on GitHub
area-integrations
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

It seems that Entity Framework is unable to create migrations when it's expecting environment variables through .NET Aspire.
I am unsure of why this is necessary.
```
~/Documents/CODE/identifisk/ApiService$ dotnet ef migrations add MigrationTest
Build started...
Build succeeded.
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: PythonApiEndpoint not found
Unable to create a 'DbContext' of type 'RuntimeType'. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[ApiService.Persistence.ApiServiceDataContext]' while attempting to activate 'ApiService.Persistence.ApiServiceDataContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
```

ApiService/Program.cs:
```cs
// Adds HTTP Client for communicating with Python.
var pythonApiEndpoint = builder.Configuration["PythonApiEndpoint"] ??
throw new InvalidOperationException("PythonApiEndpoint not found");

builder.Services.AddHttpClient(client =>
{
client.BaseAddress = new Uri(pythonApiEndpoint);
});
```

AppHost/Program.cs:
```cs
// Register services
var pythonApi = builder
.AddContainer("pythonapi", "<-- link to image container -->")
.WithHttpEndpoint(targetPort: 5000)
.WithImageTag("staging");

var postgres = builder.AddPostgres("postgres")
.WithDataVolume()
.WithPgWeb();

var postgresdb = postgres.AddDatabase("postgresdb");

builder.AddProject("apiservice")
.WithExternalHttpEndpoints()
.WithEnvironment("PythonApiEndpoint", pythonApi.GetEndpoint("http"))
.WithReference(postgresdb)
.WaitFor(pythonApi)
.WaitFor(postgresdb);

builder.Build().Run();
```

### Expected Behavior

I expect the environment variable pumped in through the AppHost to be inconsequential to Entity Framework being able to create a migration.

### Steps To Reproduce

_No response_

### Exceptions (if any)

_No response_

### .NET Version info

.NET 9

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.