Add an implementation of 'IDesignTimeDbContextFactory' to the project

asp.net asp.net-core asp.net-core-mvc asp.net-mvc entity-framework-core

Question

I Created an asp.net 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 asp.net 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 https://go.microsoft.com/fwlink/?linkid=851728 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)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}


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 =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

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

        services.AddMvc();
    }

    // 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.UseBrowserLink();
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                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());
    }
1
0
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.

3
5/24/2018 7:51:10 AM


Related Questions





Related

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