More than one DbContext named 'myproject.Models.myprojectContext' was found. Specify which one to use by providing its fully qualified name using its exact case

April 15, 2019 0 comments

I was developing a Web-Application in Asp.Net Core 2.2 . After I added the new identity with scaffold it generated me these codes:
For IdentityHostingStartup.cs




[assembly:
 HostingStartup(typeof(myproject.Areas.Identity.IdentityHostingStartup))]
namespace myproject.Areas.Identity
{
    public class IdentityHostingStartup : IHostingStartup
    {
     public void Configure(IWebHostBuilder builder)
      {
      builder.ConfigureServices((context, services) => {
      services.AddDbContext(options =>
       options.UseSqlServer(
       context.Configuration.GetConnectionString("myprojectContextConnection")));

        services.AddDefaultIdentity()
                .AddEntityFrameworkStores();
            });
        }
    }
}

Code in my Startup.cs (After adding Identity I made some modifications in startup.cs, with reference to this article)

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.Configure(options =>
            {
        // This lambda determines whether user consent for 
       //non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


 services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddRazorPagesOptions(options =>
        {
        options.AllowAreas = true;
        options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage");
        options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
        });

      services.ConfigureApplicationCookie(options =>
        {
        options.LoginPath = $"/Identity/Account/Login";
        options.LogoutPath = $"/Identity/Account/Logout";
        options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
        });

        services.Configure(Configuration.GetSection("EmailSettings"));
        services.AddSingleton();
        services.AddScoped();

        }

        // 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.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

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

This was working fine, then I added new models and wanted to scaffold a model with controller and views using Entity Framework. After I set up everything and click ok, I started getting error saying : More than one DbContext named 'myproject.Models.myprojectContext' was found. Specify which one to use by providing its fully qualified name using its exact case.

More than one DbContext was found. Specify which one to use by providing its fully qualified name using its exact case.


 To resolve this issue I removed following lines of code from IdentityHostingStartup.cs

  services.AddDbContext(options =>
   options.UseSqlServer(
   context.Configuration.GetConnectionString("myprojectContextConnection")));

And I modified my startup.cs file to following

 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.Configure(options =>
      {
      // This lambda determines whether user consent for 
      //non-essential cookies is needed for a given request.
       options.CheckConsentNeeded = context => true;
       options.MinimumSameSitePolicy = SameSiteMode.None;
      });

  services.AddDbContext(options =>
  options.UseSqlServer(Configuration
.GetConnectionString("myprojectContextConnection")));


  services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddRazorPagesOptions(options =>
    {
    options.AllowAreas = true;
    options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage");
    options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
     });

   services.ConfigureApplicationCookie(options =>
    {
    options.LoginPath = $"/Identity/Account/Login";
    options.LogoutPath = $"/Identity/Account/Logout";
    options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
     });

    services.Configure(Configuration.GetSection("EmailSettings"));
    services.AddSingleton();
    services.AddScoped();

    }

        // 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.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change 
                //this for production scenarios,
                // see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

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

Now I try generating Scaffold items it works without any error.
Share Share Tweet Share

0 thoughts on "More than one DbContext named 'myproject.Models.myprojectContext' was found. Specify which one to use by providing its fully qualified name using its exact case"

LEAVE A REPLY