Skip to content

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

@euandmj

Description

@euandmj

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.

Steps To Reproduce

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
    }
}

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)

.NET Version

10.0.100

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-area-labelUsed by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions