Search code examples
c#entity-framework-corenpgsql.net-8.0

System.MissingMethodException when Initializing a database with EntityFrameworkCore and Different Schemas


I'm encountering an issue when trying to initialize my database. I have two DbContexts, IdentityDbContext and AuctionhouseDbContext, in my application, both using the same database but different schemas. When I attempt to initialize the database using the Update-Database -Context IdentityDbContext command, I get the following exception:

System.MissingMethodException: Method not found: 'System.Data.Common.DbParameter System.Data.Common.DbBatchCommand.CreateParameter()'.
   at Npgsql.NpgsqlCommand..ctor(String cmdText, NpgsqlConnection connection)
   at Npgsql.NpgsqlCommand.CreateCachedCommand(NpgsqlConnection connection)
   at Npgsql.NpgsqlConnection.CreateCommand()
   at Npgsql.NpgsqlConnection.CreateDbCommand()
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.CreateDbCommand(RelationalCommandParameterObject parameterObject, Guid commandId, DbCommandMethod commandMethod)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.Create()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Method not found: 'System.Data.Common.DbParameter System.Data.Common.DbBatchCommand.CreateParameter()'.

Here is the relevant code for the components involved:

public class IdentityDbContext 
    : IdentityDbContext<ApplicationUser, IdentityRole<Guid>, Guid>
{
    public const string Schema = "identity";

    public IdentityDbContext(
        DbContextOptions<IdentityDbContext> options) 
        : base(options) { }

    protected override void OnModelCreating(
        ModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema(Schema);

        base.OnModelCreating(modelBuilder);
    }
}

The ServiceCollectionExtensions.cs file which configures the DbContexts like this:

var connectionString = configuration.GetConnectionString("IdentityConnection");

services.AddDbContext<IdentityDbContext>(options =>
{
    options.UseNpgsql(connectionString, options =>
    {
        options.MigrationsHistoryTable(
            tableName: HistoryRepository.DefaultTableName,
            schema: IdentityDbContext.Schema);
    });
});

Any help or guidance on how to resolve this issue during migration creation would be greatly appreciated. Thank you!


The Directory.Packages.props file looks like this:

<Project>
  <PropertyGroup>
    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
  </PropertyGroup>
  <ItemGroup>
    <PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0-rc.2.23480.2" />
    <PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.0-rc.2.23480.1" />
    <PackageVersion Include="NSwag.AspNetCore" Version="13.20.0" />
    <PackageVersion Include="NSwag.MSBuild" Version="13.20.0" />
    <PackageVersion Include="MediatR" Version="12.1.1" />
    <PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0-rc.2.23480.2" />
    <PackageVersion Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.0-rc.2.23480.2" />
    <PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0-rc.2" />
  </ItemGroup>
</Project>

The Directory.Build.props looks like this:

<Project>
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
</Project>

I couldn't find any mismatch yet. I've installed all latest pre-releases of every package, except NSwag.


Solution

  • Based on the docs DbBatchCommand.CreateParameter was introduced in .NET 8, so it seems that you have some package/runtime version mismatch. Check out that matching versions of EF Core, Npgsql.EntityFrameworkCore.PostgreSQL and Microsoft.AspNetCore.Identity.EntityFrameworkCore are installed and you are using the corresponding SKD/runtime (for example for .NET 8 - latest release candidate with matching package versions - RC 2).

    Check also MissingMethodException in Integration test with sqlite .

    UPD.

    Your project works fine for me with .NET 8 RC 2, though I have used dotnet ef tool (from src folder):

    dotnet ef database update -s WebApi/WebApi.csproj -p Identity/Identity.Infrastructure/Identity.Infrastructure.csproj -c IdentityDbContext
    

    Check our that you have latest release candidate installed.