Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No parameterless constructor was found on 'ClientsContext'. Either add a parameterless constructor to 'ClientsContext' or add an implementation of 'IDbContextFactory<ClientsContext>' in the same assembly as 'ClientsContext'. #8024

Closed
pantonis opened this issue Mar 29, 2017 · 6 comments

Comments

@pantonis
Copy link

pantonis commented Mar 29, 2017

Describe what is not working as expected.
I am using a window service with Autofac DI and when I run Add-Migration TestDD1InitialMigration I get error

No parameterless constructor was found on 'ClientsContext'. Either add a parameterless constructor to 'ClientsContext' or add an implementation of 'IDbContextFactory<ClientsContext>' in the same assembly as 'ClientsContext'.

Further technical details

EF Core version: 1.1.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017

@ajcvickers
Copy link
Member

@pantonis Can you post the code of your DbContext class?

@pantonis
Copy link
Author

using Microsoft.EntityFrameworkCore;

namespace Test.Infrastructure.EntityFramework
{
    public class ClientContext : DbContext
    {
        public ClientContext(DbContextOptions<ClientContext> options)
                : base(options)
        {
            Database.Migrate();
        }

        public DbSet<Client> Clients { get; set; }
        public DbSet<Operation> Operations { get; set; }
    }
}

@ajcvickers
Copy link
Member

@pantonis This usually means that tooling is unable to use your Startup class for some reason. The exception message in this case is confusing; we need to generate better feedback for this case.

@bricelam I was unable to find the duplicate for this, so assigning to you to de-dupe as necessary.

@ajcvickers
Copy link
Member

@pantonis Also, calling Database.Migrate in your context constructor is likely to cause issues both because it is attempting to use the context before it is fully constructed, and because it will interfere with tooling which is also trying to do Migrations things at the same time.

@bricelam
Copy link
Contributor

bricelam commented Apr 1, 2017

Dupe of #7264

@bricelam bricelam removed their assignment Apr 1, 2017
@bricelam bricelam removed this from the 2.0.0 milestone Apr 1, 2017
@bricelam bricelam closed this as completed Apr 1, 2017
@pantonis
Copy link
Author

pantonis commented Apr 1, 2017

For anyone out there having the same problem I managed to resolve it using an IDbContextFactory

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Configuration;

namespace Test.Infrastructure.EntityFramework
{
    public class ClientContextFactory : IDbContextFactory<ClientContext>
    {
        private IConfigurationRoot configuration;

        public ClientContextFactory()
        {
            var builder = new ConfigurationBuilder()
           .SetBasePath(System.AppContext.BaseDirectory)
           .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

            configuration = builder.Build();
        }

        public ClientContext Create(DbContextFactoryOptions options)
        {
            var optionsBuilder = new DbContextOptionsBuilder<ClientContext>();
            optionsBuilder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"), m => { m.EnableRetryOnFailure(); });

            return new ClientContext(optionsBuilder.Options);
        }
    }
}

The parameterized context constructor (code above) remained the same

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants