3

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.

6
  • Can you please add your .csproj file Commented Nov 1, 2023 at 18:00
  • @GuruStron First of all, big thanks! I will take a look into the packages. Never thought of a mismatch in the versions. I also included the package versions above Commented Nov 1, 2023 at 18:50
  • TBH packages look ok. Can you please post a minimal reproducible example somewhere? Commented Nov 1, 2023 at 18:58
  • 1
    @GuruStron I hope it's fine for you: github.com/maik-hasler/77404355. I can also try to make a smaller reproducible example, but at least that's the fastest way^^ Commented Nov 1, 2023 at 19:01
  • 1
    Not sure that will be able to look at it closely today but will try. Commented Nov 1, 2023 at 19:03

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! I still had the RC 1 version installed.. Urggh. Very stupid^^
@MaikHasler was glad to help! Would not call that stupid though, just unexpected - such things do not happen (or at least should not) with "ordinary" versions. But previews and RCs live by their own rules)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.