0

I have a .Net Core app with EntityFramework. I wanted to add controller with CRUD operations. But I got this error enter image description here

I don't get any error while performing add-migration. It finds only one DbContext.

These are all the places where I have WebApplication8Context

WEBAPPLICATION8CONTEXT.CS

    namespace WebApplication8.Data
{
    public class WebApplication8Context : IdentityDbContext<ApplicationUser>
    {
        public WebApplication8Context(DbContextOptions<WebApplication8Context> options)
            : base(options)
        {
        }
     

        public DbSet<Tasks> Tasks { get; set; }
        public DbSet<TaskGroups> TaskGroups { get; set; }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);

        }
    }

IDENTITYHOSTINGSTARTUP.CS

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

                services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
                    .AddEntityFrameworkStores<WebApplication8Context>();
            });
        }
    }
}

APPSETTINGS.JSON

    {
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "WebApplication8ContextConnection": "Server=CREATIVESM\\SQLEXPRESS;Database=MarApplication;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

BEGGINING OF MY WEBAPPLICATION8MODELSNAPSHOT

    namespace WebApplication8.Migrations
{
    [DbContext(typeof(WebApplication8Context))]
    partial class WebApplication8ContextModelSnapshot : ModelSnapshot
    {
        protected override void BuildModel(ModelBuilder modelBuilder)
        {
#pragma warning disable 612, 618
            modelBuilder
                .HasAnnotation("ProductVersion", "3.1.10")
                .HasAnnotation("Relational:MaxIdentifierLength", 128)
                .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

My Startup.cs

    namespace WebApplication8
{
    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.AddControllersWithViews();
            services.AddRazorPages();

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment 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.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });
        }
    }
}

Other places where I can find WebApplication8Context are my migrations.

What am I doing wrong?

1 Answer 1

1

It looks like there is a conflict between IdentityHostingStartup and Startup.

Although I don't know the specific reason, you can use the following methods to solve it:

You can delete the code in your IdentityHostingStartup,then add it to your Startup like this:

    services.AddDbContext<WebApplication8Context>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("WebApplication8ContextConnection")));
    services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores<WebApplication8Context>();
    services.AddControllersWithViews();
    services.AddRazorPages();

Re-migration and update database,then you will add the controller successful.

Sign up to request clarification or add additional context in comments.

1 Comment

Adding code to startup instead of identityhostingstartup worked :) thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.