1

I have a DbContext that I am using to create a design time migration however I am having trouble.

I have a "Migractions" class (Below) that is essentially a Designtime factory as per this answer.

No errors and it builds.

I have placed this class in the web project and the context is in another project library.

I run the migration using the following:

dotnet ef migrations add INITIAL --context TENANTSContext -s ../Jobsledger.API

Once it runs it builds and then the following displays:

Build started...
Build succeeded.
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: JobsLedger.DATABASE.CREATION.SERVICE.IDatabaseCreationService Lifetime: Scoped ImplementationType: JobsLedger.DATABASE.CREATION.SERVICE.DatabaseCreationService': No constructor for type 'JobsLedger.TENANTS.TENANTSContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATABASE.CREATION.SERVICE.Interfaces.INewDatabaseService Lifetime: Scoped ImplementationType: JobsLedger.DATABASE.CREATION.SERVICE.NewDatabaseService': Unable to resolve service for type 'JobsLedger.TENANTS.Repositories.Interfaces.IAccountTENANTSRepository' while attempting to activate 'JobsLedger.DATABASE.CREATION.SERVICE.NewDatabaseService'.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATABASE.CREATION.SERVICE.Interfaces.ITenantAndAccountInitialisations Lifetime: Scoped ImplementationType: JobsLedger.DATABASE.CREATION.SERVICE.TenantAndAccountInitialisations': No constructor for type 'JobsLedger.TENANTS.TENANTSContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.TENANTS.TENANTSContext Lifetime: Scoped ImplementationType: JobsLedger.TENANTS.TENANTSContext': No constructor for type 'JobsLedger.TENANTS.TENANTSContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.DATAContext Lifetime: Scoped ImplementationType: JobsLedger.DATA.DATAContext': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.IBrandDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.BrandDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.IClientDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.ClientDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.ICounterDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.CounterDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.IJobDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.JobDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.ILoggingDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.LoggingDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.INoteDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.NoteDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.INoteTypeDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.NoteTypeDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.IStateDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.StateDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.IStatusDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.StatusDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.ISuburbDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.SuburbDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.ITypeDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.TypeDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.IVisitDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.VisitDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.TENANTS.Repositories.Interfaces.ITenantTENANTSRepository Lifetime: Scoped ImplementationType: JobsLedger.TENANTS.Repositories.TenantTENANTSRepository': No constructor for type 'JobsLedger.TENANTS.TENANTSContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.TENANTS.Repositories.Interfaces.ITenantTENANTSRepository Lifetime: Scoped ImplementationType: JobsLedger.TENANTS.Repositories.TenantTENANTSRepository': No constructor for type 'JobsLedger.TENANTS.TENANTSContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.TENANTS.Repositories.Interfaces.ICounterTENANTSRepository Lifetime: Scoped ImplementationType: JobsLedger.TENANTS.Repositories.CounterTENANTSRepository': No constructor for type 'JobsLedger.TENANTS.TENANTSContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.TENANTS.Repositories.Interfaces.ISuburbTENANTSRepository Lifetime: Scoped ImplementationType: JobsLedger.TENANTS.Repositories.SuburbTENANTSRepository': No constructor for type 'JobsLedger.TENANTS.TENANTSContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.TENANTS.Repositories.Interfaces.IStateTENANTSRepository Lifetime: Scoped ImplementationType: JobsLedger.TENANTS.Repositories.StateTENANTSRepository': No constructor for type 'JobsLedger.TENANTS.TENANTSContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.API.CONTROLLER.SERVICES.API.App.ClientService.Interfaces.IClientServices Lifetime: Scoped ImplementationType: JobsLedger.API.CONTROLLER.SERVICES.API.App.ClientService.ClientServices': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.API.CONTROLLER.SERVICES.API.App.JobService.Interfaces.IJobServices Lifetime: Scoped ImplementationType: JobsLedger.API.CONTROLLER.SERVICES.API.App.JobService.JobServices': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.API.CONTROLLER.SERVICES.Authorisation.Interfaces.IAccountService Lifetime: Scoped ImplementationType: JobsLedger.API.CONTROLLER.SERVICES.Authorisation.AccountService': Unable to resolve service for type 'JobsLedger.TENANTS.Repositories.Interfaces.IAccountTENANTSRepository' while attempting to activate 'JobsLedger.API.CONTROLLER.SERVICES.Authorisation.AccountService'.) (Error while validating the service descriptor 'ServiceType: JobsLedger.API.CONTROLLER.SERVICES.Common.Interfaces.IAddressDropdownServices Lifetime: Scoped ImplementationType: JobsLedger.API.CONTROLLER.SERVICES.Common.AddressDropdownServices': Unable to resolve service for type 'JobsLedger.INTERFACES.IUserSession' while attempting to activate 'JobsLedger.API.CONTROLLER.SERVICES.Common.AddressDropdownServices'.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATABASE.CREATION.SERVICE.Interfaces.ITenantAndAccountInitialisations Lifetime: Scoped ImplementationType: JobsLedger.DATABASE.CREATION.SERVICE.TenantAndAccountInitialisations': No constructor for type 'JobsLedger.TENANTS.TENANTSContext' can be instantiated using services from the service container and default values.)
System.MissingMethodException: No parameterless constructor defined for type 'JobsLedger.API.DesignTimeTENANTSContextFactory'.
   at System.RuntimeType.CreateInstanceDefaultCtorSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContextFromFactory(Type factory, Type contextType)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_2.<FindContextTypes>b__9()
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
No parameterless constructor defined for type 'JobsLedger.API.DesignTimeTENANTSContextFactory'.

The first part of that error is detailed again below and says ALL scoped services cannot be resolved - these are essentially a list of all services for the DI in my startup:

An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: JobsLedger.DATABASE.CREATION.SERVICE.IDatabaseCreationService Lifetime: Scoped ImplementationType: JobsLedger.DATABASE.CREATION.SERVICE.DatabaseCreationService': No constructor for type 'JobsLedger.TENANTS.TENANTSContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATABASE.CREATION.SERVICE.Interfaces.INewDatabaseService Lifetime: Scoped ImplementationType: JobsLedger.DATABASE.CREATION.SERVICE.NewDatabaseService': Unable to resolve service for type 'JobsLedger.TENANTS.Repositories.Interfaces.IAccountTENANTSRepository' while attempting to activate 'JobsLedger.DATABASE.CREATION.SERVICE.NewDatabaseService'.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATABASE.CREATION.SERVICE.Interfaces.ITenantAndAccountInitialisations Lifetime: Scoped ImplementationType: JobsLedger.DATABASE.CREATION.SERVICE.TenantAndAccountInitialisations': No constructor for type 'JobsLedger.TENANTS.TENANTSContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.TENANTS.TENANTSContext Lifetime: Scoped ImplementationType: JobsLedger.TENANTS.TENANTSContext': No constructor for type 'JobsLedger.TENANTS.TENANTSContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.DATAContext Lifetime: Scoped ImplementationType: JobsLedger.DATA.DATAContext': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.IBrandDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.BrandDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.IClientDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.ClientDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.ICounterDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.CounterDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.IJobDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.JobDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.ILoggingDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.LoggingDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.INoteDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.NoteDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.INoteTypeDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.NoteTypeDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.IStateDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.StateDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.IStatusDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.StatusDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.ISuburbDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.SuburbDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.ITypeDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.TypeDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATA.Repositories.Interfaces.IVisitDATARepository Lifetime: Scoped ImplementationType: JobsLedger.DATA.Repositories.VisitDATARepository': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.TENANTS.Repositories.Interfaces.ITenantTENANTSRepository Lifetime: Scoped ImplementationType: JobsLedger.TENANTS.Repositories.TenantTENANTSRepository': No constructor for type 'JobsLedger.TENANTS.TENANTSContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.TENANTS.Repositories.Interfaces.ITenantTENANTSRepository Lifetime: Scoped ImplementationType: JobsLedger.TENANTS.Repositories.TenantTENANTSRepository': No constructor for type 'JobsLedger.TENANTS.TENANTSContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.TENANTS.Repositories.Interfaces.ICounterTENANTSRepository Lifetime: Scoped ImplementationType: JobsLedger.TENANTS.Repositories.CounterTENANTSRepository': No constructor for type 'JobsLedger.TENANTS.TENANTSContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.TENANTS.Repositories.Interfaces.ISuburbTENANTSRepository Lifetime: Scoped ImplementationType: JobsLedger.TENANTS.Repositories.SuburbTENANTSRepository': No constructor for type 'JobsLedger.TENANTS.TENANTSContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.TENANTS.Repositories.Interfaces.IStateTENANTSRepository Lifetime: Scoped ImplementationType: JobsLedger.TENANTS.Repositories.StateTENANTSRepository': No constructor for type 'JobsLedger.TENANTS.TENANTSContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.API.CONTROLLER.SERVICES.API.App.ClientService.Interfaces.IClientServices Lifetime: Scoped ImplementationType: JobsLedger.API.CONTROLLER.SERVICES.API.App.ClientService.ClientServices': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.API.CONTROLLER.SERVICES.API.App.JobService.Interfaces.IJobServices Lifetime: Scoped ImplementationType: JobsLedger.API.CONTROLLER.SERVICES.API.App.JobService.JobServices': No constructor for type 'JobsLedger.DATA.DATAContext' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: JobsLedger.API.CONTROLLER.SERVICES.Authorisation.Interfaces.IAccountService Lifetime: Scoped ImplementationType: JobsLedger.API.CONTROLLER.SERVICES.Authorisation.AccountService': Unable to resolve service for type 'JobsLedger.TENANTS.Repositories.Interfaces.IAccountTENANTSRepository' while attempting to activate 'JobsLedger.API.CONTROLLER.SERVICES.Authorisation.AccountService'.) (Error while validating the service descriptor 'ServiceType: JobsLedger.API.CONTROLLER.SERVICES.Common.Interfaces.IAddressDropdownServices Lifetime: Scoped ImplementationType: JobsLedger.API.CONTROLLER.SERVICES.Common.AddressDropdownServices': Unable to resolve service for type 'JobsLedger.INTERFACES.IUserSession' while attempting to activate 'JobsLedger.API.CONTROLLER.SERVICES.Common.AddressDropdownServices'.) (Error while validating the service descriptor 'ServiceType: JobsLedger.DATABASE.CREATION.SERVICE.Interfaces.ITenantAndAccountInitialisations Lifetime: Scoped ImplementationType: JobsLedger.DATABASE.CREATION.SERVICE.TenantAndAccountInitialisations': No constructor for type 'JobsLedger.TENANTS.TENANTSContext' can be instantiated using services from the service container and default values.)

Here is my Migration.cs class that is the design time factory:

    public DesignTimeTENANTSContextFactory(IDbConnectionString connectionString) 
    {
        _connectionString = connectionString;
    }
    public TENANTSContext CreateDbContext(string[] args) 
    {
        var configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .Build();

        var builder = new DbContextOptionsBuilder<TENANTSContext>();

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

        builder.UseSqlServer(connectionString);

        return new TENANTSContext(builder.Options);
    }
}

Its using a parameterless constructor in the TENANTSContext which is detailed below:

public class TENANTSContext : DbContext
{
    private readonly HttpContext _httpContext;

    // Constructors..
    public TENANTSContext(DbContextOptions<TENANTSContext> options)
        : base(options)
    { }

    public TENANTSContext(DbContextOptions<TENANTSContext> options, HttpContext httpContext) : base(options)
    {
        _httpContext = httpContext;
    }
    
    public DbSet<Tenant> Tenants { get; set; }

    ......

So why am I getting the above error. I am also following this documentation This says it doesnt use application services and instead uses a designTimeFactory which is exactly what I am using so why is it complaining about all the services that cannot be instatiated etc.

Also, what does it mean by:

No parameterless constructor defined for type 'JobsLedger.API.DesignTimeTENANTSContextFactory'.

Basically what's wrong and how do I correct it?

si2030
  • 3,895
  • 8
  • 38
  • 87
  • 1
    They are expecting your design time factory class to have parameterless constructor (i.e. constructor w/o parameters) . While your factory class has constructor with parameter - `public DesignTimeTENANTSContextFactory(IDbConnectionString connectionString)` – Ivan Stoev Nov 09 '20 at 12:57
  • I used HttpContextAccessor with services.AddHttpContextAccessor(); – Imran Qadir Baksh - Baloch Aug 18 '21 at 06:22

0 Answers0