Home > Software engineering >  How to fix An exception was thrown while invoking the constructor 'Void .ctor
How to fix An exception was thrown while invoking the constructor 'Void .ctor

Time:11-27

I try to add-migration to one of my contexts but i find this error

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)

I have DefaultInfrastructureModule code like this:

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

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

And my AppDbContext like this:

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>();
.
.
.
}

How to fix this exception, Note that I want to dealing with 3 source Database.

CodePudding user response:

Finally I fix it in my case with change MySql.EntityFrameworkCore package to Pomelo.EntityFrameworkCore.MySql and it's worked successfully.

  • Related