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

Related Articles

IntelliSense in C#
In this video, I explain how to ensure that your …

IntelliSense In C# using VsCode (OmniSharp)

looking for a nice training platform
Have you thought about starting a blog and lost in …

Blogging And Some Tips

About The Author

Samson Amaugo

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

12 Comments

  1. Ticket- SENDING 0,75406835 BTC. Receive => https://graph.org/GET-BITCOIN-TRANSFER-02-23-2?hs=b540c367fb6e7c063008248de0e5deb6&

    ttt2ku

    March 10, 2025
  2. Sending a transfer from our company. Take > https://graph.org/GET-BITCOIN-TRANSFER-02-23-2?hs=b540c367fb6e7c063008248de0e5deb6&

    c631xr

    March 16, 2025
  3. Hazel, here's a summary of your BTC activity. https://graph.org/Message--17856-03-25?hs=b540c367fb6e7c063008248de0e5deb6&

    f95sdz

    March 27, 2025
  4. + 1.673726 BTC.GET - https://graph.org/Message--120154-03-25?hs=b540c367fb6e7c063008248de0e5deb6&

    px3tv5

    March 30, 2025
  5. + 1.288205 BTC.NEXT - https://graph.org/Message--04804-03-25?hs=b540c367fb6e7c063008248de0e5deb6&

    uttqx4

    April 2, 2025
  6. + 1.305838 BTC.GET - https://graph.org/Message--04804-03-25?hs=b540c367fb6e7c063008248de0e5deb6&

    3yefil

    April 8, 2025
  7. Message: SENDING 1.252385 bitcoin. Confirm > https://graph.org/Message--04804-03-25?hs=b540c367fb6e7c063008248de0e5deb6&

    i4eycg

    April 9, 2025
  8. Notification: SENDING 1.673592 bitcoin. Assure > https://graph.org/Message--05654-03-25?hs=b540c367fb6e7c063008248de0e5deb6&

    ez7je2

    April 14, 2025
  9. Notification; + 1,91069 BTC. Receive >>> https://graph.org/Message--17856-03-25?hs=b540c367fb6e7c063008248de0e5deb6&

    idmttg

    April 16, 2025
  10. + 1.794029 BTC.GET - https://graph.org/Message--0484-03-25?hs=b540c367fb6e7c063008248de0e5deb6&

    ojm163

    April 26, 2025
  11. + 1.622702 BTC.NEXT - https://graph.org/Ticket--58146-05-02?hs=b540c367fb6e7c063008248de0e5deb6&

    yxk0lq

    May 4, 2025
  12. Ticket; TRANSFER 1,708699 bitcoin. Next => https://graph.org/Ticket--58146-05-02?hs=b540c367fb6e7c063008248de0e5deb6&

    4esdre

    May 8, 2025

Leave a Reply

Cancel reply

Search Site

Recent Posts

  • Running Entity Framework Migrations in an Aspire-Bootstrapped Orleans Project
  • Using XDebug in Laravel Sail (VSCODE)
  • Custom Redis Streams Provider for Orleans
  • Creating a Custom File Storage Provider for Microsoft Orleans
  • Incremental Generators In C#

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 Swacblooms

  • Running Entity Framework Migrations in an Aspire-Bootstrapped Orleans Project
  • Using XDebug in Laravel Sail (VSCODE)
  • Custom Redis Streams Provider for Orleans
  • Creating a Custom File Storage Provider for Microsoft Orleans
  • Incremental Generators In C#
  • Hot Chocolate Data Loader: A Quick Guide
  • Exploring Policy-Based Authorization in Minimal API with .NET 8
  • Using Cloud Firestore in Blazor WebAssembly
  • Serving A VueJs App from DotNet
  • Dynamic Subscriptions in Hot Chocolate 🔥🍫

Swacblooms🦋

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