Swacblooms🦋

Making the Moves
Menu
  • Home
  • Motivation
  • Education
  • Programming
  • About
  • Contact
  • Privacy Policy
Home
Programming
Running Entity Framework Migrations in an Aspire-Bootstrapped Orleans Project
Programming

Running Entity Framework Migrations in an Aspire-Bootstrapped Orleans Project

Samson Amaugo March 10, 2025

Hi guys 👋🏾, so you know that Aspire Aspire Aspire has been the buzz in the DotNet world, and it does simplify setting up your development environment.

I tried bootstrapping an ASP.NET cohosted Orleans project using the Orleans and the PostgreSQL integration. When I tried to add a new migration, errors were thrown because the Orleans integration required some environment variables to be available. Those variables are only available and injected when running your project through the Aspire AppHost project.

The code below is a snippet of the initial state of the code that resorted to errors when I tried to run my entity framework migrations.

builder.Services.AddDbContextPool<AppDbContext>(options =>
{
    options.UseNpgsql(builder.Configuration.GetConnectionString("postgresdb")
        ,npgsqlOptionsAction: handleDbRetry()).UseExceptionProcessor();
});
builder.AddKeyedRedisClient("redis");
builder.UseOrleans(siloBuilder =>
{
      siloBuilder.Services.AddPooledDbContextFactory<AppDbContext>(options =>
      {
           options.UseNpgsql(builder.Configuration.GetConnectionString("postgresdb"),
               npgsqlOptionsAction: handleDbRetry());
      });
 });

When I execute dotnet ef migrations add [name of migration], the error snippet below is thrown

An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Orleans.Runtime.MembershipService.MembershipTableManager Lifetime: Singleton ImplementationType: Orleans.Runtime.MembershipService.MembershipTableManager':

To prevent this, I resorted to always commenting out the builder.UseOrleans block when I want to run migrations. And it worked but I couldn’t see myself always doing this moving forward. So I did some research and discovered that the migration command doesn’t inject any special environment variable when trying to add a migration.

I also discovered that you could add an environment variable which could be analysed before the builder.UseOrleans block runs. This works but I didn’t want to always add extra commands aside from executing the normal dotnet ef migration add command.

Finally, I found a way on Stackoverflow to detect if the dotnet ef migrations add command is executed 💡.

All I needed to do was check if a particular letter combination was available in the executable file name or command line arguments tied to the dotnet ef migrations add process. The Environment.GetCommandLineArgs() method allows you to retrieve this and perform desired checks.

My final code block that does this seamlessly with the same dotnet ef migrations add command can be seen below.

var isMigrations = Environment.GetCommandLineArgs()[0].Contains("ef.dll");
builder.Services.AddDbContextPool<AppDbContext>(options =>
{
    options.UseNpgsql(builder.Configuration.GetConnectionString("postgresdb")
        ,npgsqlOptionsAction: handleDbRetry()).UseExceptionProcessor();
});
builder.AddKeyedRedisClient("redis");
if (!isMigrations)
{
    builder.UseOrleans(siloBuilder =>
    {
        siloBuilder.Services.AddPooledDbContextFactory<AppDbContext>(options =>
        {
            options.UseNpgsql(builder.Configuration.GetConnectionString("postgresdb"),
                npgsqlOptionsAction: handleDbRetry());
        });
    });
}

I just needed to check if the executable file name contained (“ef.dll”), if it did then that meant I was running a migration and there was no need to configure or set up the Orleans silo.

Thanks for reading. Bye ✌🏾

Prev Article
Next Article

Related Articles

Cloudflare Tunnel to Postgres, plus Swarm's static IP problem
I wanted to query a Postgres database on my VPS …

Cloudflare Tunnel to Postgres, plus Swarm’s static IP problem

easyssh
Hola 🖐….. Sometimes you might find it hard to keep …

Easy SSH Via Local DNS

About The Author

Samson Amaugo

I am Samson Amaugo. I am a full-stack developer and I specialize in DotNet and the MERN stack.

Search Site

Recent Posts

  • Cloudflare Tunnel to Postgres, plus Swarm’s static IP problem
  • The Topological Sort
  • Building a Perceptron in .NET
  • Part 4 — Assertions: Verifying What You See
  • Part 3 — Actions and Auto-Waiting: Putting Locators to Work

Categories

  • EDUCATION
  • Motivation
  • Programming
  • Uncategorized

Get more stuff

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

we respect your privacy and take protecting it seriously

RSS feed: Swacblooms Swacblooms

Swacblooms🦋

Making the Moves
Copyright © 2026 Swacblooms🦋
Swacblooms - Making the Moves