【问题标题】:How to fix An exception was thrown while invoking the constructor 'Void .ctor如何修复调用构造函数“Void .ctor”时引发异常
【发布时间】:2021-11-24 22:03:44
【问题描述】:

我尝试将迁移添加到我的一个上下文中,但我发现了这个错误

Autofac.Core.DependencyResolutionException: An exception was thrown while activating weno.Infrastructure.Data.DataContext.AppDbContext.

 ---> Autofac.Core.DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions`1
[weno.Infrastructure.Data.DataContext.AppDbContext], MediatR.IMediator)' on type 'AppDbContext'.

 ---> System.TypeLoadException: Method 'AppendIdentityWhereCondition' in type 'MySql.EntityFrameworkCore.MySQLUpdateSqlGenerator'
 from assembly 'MySql.EntityFrameworkCore, Version=5.0.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' 
does not have an implementation.
   at MySql.EntityFrameworkCore.Extensions.MySQLServiceCollectionExtensions.<>c.<AddEntityFrameworkMySQL>b__0_3(ServiceCollectionMap m)

我有这样的 DefaultInfrastructureModule 代码:

builder.RegisterGeneric(typeof(AppRepository<>))
        .As(typeof(IRepository<>))
        .As(typeof(IReadRepository<>))
        .InstancePerLifetimeScope();

    builder.RegisterGeneric(typeof(AccountingRepository<>))
        .As(typeof(IRepository<>))
        .As(typeof(IReadRepository<>))
        .InstancePerLifetimeScope();
.
.
.
}

我的 AppDbContext 是这样的:

public class AppDbContext : DbContext
{
  private readonly IMediator? _mediator;

  public AppDbContext(DbContextOptions<AppDbContext> options, IMediator? mediator)
      : base(options)
  {
    _mediator = mediator;
  }

  public DbSet<ToDoItem> ToDoItems => Set<ToDoItem>();
  public DbSet<Project> Projects => Set<Project>();
.
.
.
}

如何修复此异常, 请注意,我要处理 3 源数据库。

【问题讨论】:

    标签: c# asp.net-core domain-driven-design


    【解决方案1】:

    最后我通过将 MySql.EntityFrameworkCore 包更改为 Pomelo.EntityFrameworkCore.MySql 来修复它,并且成功运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多