Add an implementation of 'IDesignTimeDbContextFactory' to the project entity-framework-core


I Created an core project And I selected UseIndiviualUserAccount As My AutenticationType, After preparing project by visual studio i got some prebuilt class and controller, Obviously i got migration folder and its config since i dont want to use defulat structure of core ,i deleted the migration folder and i created a new class library and i Rename it to MyProject.Core. in MyProject.Core Class Library i create my db models and dbContext classes,so i need to run the add-migration "init" command for creating my database so i did but i got below error, Note : i am running the add migration command in Myproject.Core in PMC !! Unable to create an object of type 'ApplicationDbContext'. Add an implementation of 'IDesignTimeDbContextFactory' to the project, or see for additional patterns supported at design time.

how can i fix this error, i read some article in current site and the other website but i cant fix my problem.

My project Structure

public class Program
    public static void Main(string[] args)

    public static IWebHost BuildWebHost(string[] args) =>

public class Startup
    public Startup(IConfiguration configuration)
        Configuration = configuration;

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
        services.AddTransient<IUnitOfWork, UnitOfWork>();
        services.AddTransient<IProvinceRepository, ProvinceRepository>();
        services.AddTransient<IBrandRepository, BrandRepository>();

        services.AddDbContext<ApplicationDbContext>(options =>

        services.AddIdentity<ApplicationUser, IdentityRole>()

        // Add application services.
        services.AddTransient<IEmailSender, EmailSender>();


    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        if (env.IsDevelopment())



        app.UseMvc(routes =>
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");

public class ApplicationDbContext : IdentityDbContext<ApplicationUser,ApplicationRole,string>
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options): base(options)
    static ApplicationDbContext()
        // Set the database intializer which is run once during application start
        // This seeds the database with admin user credentials and admin role
        // Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
5/24/2018 6:46:51 AM

Popular Answer

This can happen when EF don't know where the entry point to your application is. You do not need to implement IDesignTimeDbContextFactory if you're using BuildWebHost. First of all, make sure that you are running your migration from the project containing the migrations.

Eg... if you're using Package Manager Console:

cd .\MyProject.Core

Then run your migration using startup-project and point it to the project with BuildWebHost.

dotnet ef --startup-project ../MyProject.Web/ migrations add Initial

Otherwise EF will not know where your implementation is.

5/24/2018 7:51:10 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow