0
votes

I am trying to create an Angular 8 project with asp.net Core 2.1. Whenever I try to add migration using command

cmd command: dotnet ef migrations add init --project ../Lgn.DAL

The terminal throws error : 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.

Startup.cs

`` 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.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    // 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.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }
        app.UseCors(builder =>
        {
            builder.WithOrigins("http://localhost:4200");
            builder.AllowAnyMethod();
            builder.AllowAnyHeader();
        });

        app.UseHttpsRedirection();
        app.UseMvc();
    }
}``
1

1 Answers

1
votes

Take a look at this solution to someone with a similar issue. Is your dependency injection setup all good? (number 2 on that list)

Here are the things to consider:

You get that error because to generate migrations you need either:

  • A DbContext with a default constructor (that is, a parameterless constructor)
  • Being able to get the DbContext from ApplicationServices (that is, Dependency Injection)
  • A design time factory that returns a properly configured DbContext.