CleanArchitecture icon indicating copy to clipboard operation
CleanArchitecture copied to clipboard

"A synchronous store management operation was performed..." when using AddAsyncSeeding with EF Core CLI

Open fadiroot opened this issue 7 months ago • 2 comments

When running dotnet ef database update on a project that uses AddAsyncSeeding in OnConfiguring, the following error occurs:

System.InvalidOperationException: A synchronous store management operation was performed and no synchronous seed delegate has been provided, however an asynchronous seed delegate was. Set 'UseSeeding' option with a delegate equivalent to the one supplied in 'UseAsyncSeeding'.

Image

fadiroot avatar May 24 '25 22:05 fadiroot

not sure if this is the best solution but replace this code in InitialiserExtensions.cs

public static void AddAsyncSeeding(this DbContextOptionsBuilder builder, IServiceProvider serviceProvider)
    {
        builder.UseAsyncSeeding(async (context, _, ct) =>
        {
            var initialiser = serviceProvider.GetRequiredService<ApplicationDbContextInitialiser>();
            await initialiser.SeedAsync();
        });

        builder.UseSeeding((context, _) =>
        {
            var initialiser = serviceProvider.GetRequiredService<ApplicationDbContextInitialiser>();
            // Block on async call for sync context
            initialiser.SeedAsync().GetAwaiter().GetResult();
        });
    }

Arggon avatar May 30 '25 16:05 Arggon

@Arggon , I solved the problem using the same technique, and it worked successfully.

fadiroot avatar Jun 02 '25 08:06 fadiroot

There is a bug in the template. When implementing Data Seeding (EF Core 9+), it is necessary to implement both UseAsyncSeeding and UseSeeding, the template is only implementing UseAsyncSeeding and is therefore causing the System.InvalidOperationException.

For now, the above solution is a good workaround. I'll release a bugfix soon, however I'll also take a look at the current database initialization and seeding strategy as I feel further improvements can be made.

jasontaylordev avatar Jul 16 '25 21:07 jasontaylordev

I've resolved this issue by reverting to the previous, and simpler, initialization and seeding strategy.

jasontaylordev avatar Jul 22 '25 21:07 jasontaylordev