Skip to content

System.MissingMethodException: Method not found: 'System.String on Microsoft.EntityFrameworkCore.Design version 10.0.0 #37232

@euandmj

Description

@euandmj

Bug description

Is there an existing issue for this?

I have searched the existing issues

Describe the bug

Affects

Microsoft.EntityFrameworkCore.Design Version="10.0.0"

Exception see during a Migration Worker service after upgrading Microsoft.EntityFrameworkCore.Design to version 10.

The following function is used to ensure the database is created.

private static async Task EnsureDatabaseCreatedAsync(DbContext dbContext, CancellationToken cancellationToken)
    {
        var dbCreator = dbContext.GetService<IRelationalDatabaseCreator>();

        var strategy = dbContext.Database.CreateExecutionStrategy();
        await strategy.ExecuteAsync(async () =>
        {
            if (!await dbCreator.ExistsAsync(cancellationToken))
            {
                await dbCreator.CreateAsync(cancellationToken);
            }
        });
    }

Called in context

var mainDbContext = new SomeDbContext(
      new DbContextOptionsBuilder<SomeDbContext>()
          .UseNpgsql(configuration.GetConnectionString("db"), contextOptionsBuilder =>
              contextOptionsBuilder.ConfigureDataSource(dataSourceBuilder =>
                  dataSourceBuilder.EnableDynamicJson()
                      .EnableAzureActiveDirectoryAuthentication()))
          .UseAsyncSeeding(async (context, _, cancellationToken) =>
          {
              if (environment.IsDevelopment())
              {
                  await SeedDataAsync(context, cancellationToken);
              }
          })
          .Options);

  await EnsureDatabaseCreatedAsync(mainDbContext, stoppingToken);
  await RunMigrationAsync(mainDbContext, stoppingToken);

The following exception is seen on line var dbCreator = dbContext.GetService<IRelationalDatabaseCreator>();

System.MissingMethodException: Method not found: 'System.String

Downgrading Microsoft.EntityFrameworkCore.Design to Version="9.0.11" resolves this issue.

I tried searching https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-10.0/breaking-changes

Expected Behavior

IRelationalDatabaseCreator is resolved.

.NET Version

10.0.100
Anything else?

No response

Your code

public class MigrationService(IServiceProvider serviceProvider, IHostApplicationLifetime hostApplicationLifetime)
    : BackgroundService
{
    public const string ActivitySourceName = "Migrations";

    private static readonly ActivitySource ActivitySource = new(ActivitySourceName);

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        using var activity = ActivitySource.StartActivity("Migrating database", ActivityKind.Client);

        try
        {
            using var scope = serviceProvider.CreateScope();

            var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
            var environment = scope.ServiceProvider.GetRequiredService<IHostEnvironment>();

            var mainDbContext = new SomeDbContext(
                new DbContextOptionsBuilder<SomeDbContext>()
                    .UseNpgsql(configuration.GetConnectionString("db"), contextOptionsBuilder =>
                        contextOptionsBuilder.ConfigureDataSource(dataSourceBuilder =>
                            dataSourceBuilder.EnableDynamicJson()
                                .EnableAzureActiveDirectoryAuthentication()))
                    .UseAsyncSeeding(async (context, _, cancellationToken) =>
                    {
                        if (environment.IsDevelopment())
                        {
                            await SeedDataAsync(context, cancellationToken);
                        }
                    })
                    .Options);

            await EnsureDatabaseCreatedAsync(mainDbContext, stoppingToken);
            await RunMigrationAsync(mainDbContext, stoppingToken);
        }
        catch (Exception ex)
        {
            activity?.AddException(ex);
            throw;
        }

        hostApplicationLifetime.StopApplication();
    }

    private static async Task EnsureDatabaseCreatedAsync(DbContext dbContext, CancellationToken cancellationToken)
    {
        var dbCreator = dbContext.GetService<IRelationalDatabaseCreator>();

        var strategy = dbContext.Database.CreateExecutionStrategy();
        await strategy.ExecuteAsync(async () =>
        {
            if (!await dbCreator.ExistsAsync(cancellationToken))
            {
                await dbCreator.CreateAsync(cancellationToken);
            }
        });
    }

    private static async Task RunMigrationAsync(DbContext dbContext, CancellationToken cancellationToken)
    {
        var strategy = dbContext.Database.CreateExecutionStrategy();
        await strategy.ExecuteAsync(async () =>
        {
            await dbContext.Database.MigrateAsync(cancellationToken);
        });
    }

    private static async Task SeedDataAsync(SitesDbContext context, CancellationToken cancellationToken)
    {
// obscured
    }
}

Stack traces

Exceptions (if any)

System.MissingMethodException: Method not found: 'System.String Microsoft.EntityFrameworkCore.Diagnostics.AbstractionsStrings.ArgumentIsEmpty(System.Object)'.
         at Microsoft.EntityFrameworkCore.Utilities.Check.NullButNotEmpty(String value, String parameterName)
         at Microsoft.EntityFrameworkCore.NpgsqlModelBuilderExtensions.CanSetHiLoSequence(IConventionModelBuilder modelBuilder, String name, String schema, Boolean fromDataAnnotation)
         at Microsoft.EntityFrameworkCore.NpgsqlModelBuilderExtensions.HasHiLoSequence(IConventionModelBuilder modelBuilder, String name, String schema, Boolean fromDataAnnotation)
         at Microsoft.EntityFrameworkCore.NpgsqlModelBuilderExtensions.HasValueGenerationStrategy(IConventionModelBuilder modelBuilder, Nullable`1 valueGenerationStrategy, Boolean fromDataAnnotation)

Verbose output


EF Core version

10.0.0

Database provider

Microsoft.EntityFramework.Postgres

Target framework

.NET 10

Operating system

Mac arm

IDE

Rider

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions