使用EntityFrameworkCore和不同的模式初始化数据库时出现System.MissingMethodException异常。

3
我在尝试初始化数据库时遇到了一个问题。在我的应用程序中,我有两个DbContext,一个是IdentityDbContext,另一个是AuctionhouseDbContext,它们都使用同一个数据库但不同的模式。当我尝试使用"Update-Database -Context IdentityDbContext"命令来初始化数据库时,我会收到以下异常信息:
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()'.

这是涉及到的组件的相关代码:
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);
    }
}

这个 ServiceCollectionExtensions.cs 文件用于配置 DbContexts,就像这样:
var connectionString = configuration.GetConnectionString("IdentityConnection");

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

任何关于如何在迁移创建过程中解决此问题的帮助或指导将不胜感激。谢谢!
Directory.Packages.props 文件的内容如下:
<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>
Directory.Build.props的内容如下:
<Project>
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
</Project>

我还没有找到任何不匹配的地方。我已经安装了每个软件包的最新预发布版本,除了NSwag。

请问你能否添加你的.csproj文件? - undefined
1
@GuruStron,希望这对你来说没问题:https://github.com/maik-hasler/77404355。我也可以尝试制作一个更小的可重现示例,但至少这是最快的方式^^ - undefined
1
今天不确定能否仔细查看,但会尽力。 - undefined
顺便问一下,你能否附上 dotnet --list-sdks 的输出结果? - undefined
请查看更新的答案。 - undefined
显示剩余2条评论
1个回答

1
根据文档显示,DbBatchCommand.CreateParameter 是在.NET 8中引入的,所以看起来你的包/运行时版本不匹配。请检查EF Core、Npgsql.EntityFrameworkCore.PostgreSQLMicrosoft.AspNetCore.Identity.EntityFrameworkCore 是否安装了匹配的版本,并且你正在使用相应的SDK/运行时(例如对于.NET 8 - 最新的发布候选版本,与包版本匹配 - RC 2)。
还请检查在使用sqlite的集成测试中出现MissingMethodException更新。

对我来说,您的项目在.NET 8 RC 2上运行良好,尽管我使用了dotnet ef工具(来自src文件夹):

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

请确保您已安装最新的候选版本。

非常感谢!我还安装着RC 1版本.. 唉。真是太愚蠢了^^ - undefined
1
@MaikHasler很高兴能帮忙!虽然我不会说那是愚蠢的,只是出乎意料——这种事情在“普通”版本中是不会发生的(或者至少不应该)。但是预览版和候选版本有着它们自己的规则。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接