locked
Error System.ArgumentException: 'AddDbContext was called with configuration, but the context type 'AttendanceDbContext' only declares a parameterless constructor. RRS feed

  • Question

  • User-1355965324 posted

    I  have added the following code in my startup.cs file. But  the following error is coming in the first line

    System.ArgumentException: 'AddDbContext was called with configuration, but the context type 'AttendanceDbContext' only declares a parameterless constructor. This means that the configuration passed to AddDbContext will never be used. If configuration is passed to AddDbContext, then 'AttendanceDbContext' should declare a constructor that accepts a DbContextOptions<AttendanceDbContext> and must pass it to the base constructor for DbContext.' Please help with suggested code to fix

    my Startup class

    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<AttendanceDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ConnectionString")));
                //services.AddDbContextPool<AttendanceDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
                services.AddSession(options =>
                {
                    options.IdleTimeout = TimeSpan.FromHours(5);
                });
                services.AddMvc();
                services.Configure<ConnectionSetting>(Configuration.GetSection("ConnectionSetting"));
                services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
                services.AddOptions();
            }

    My AttendanceDbContext is as follows

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using MyApp.GO.Common;
    using MyApp.GO.Common.Models;
    using Microsoft.EntityFrameworkCore;
    using MyApp.GO.UI.Models;
    
    namespace MyApp.GO.UI.DBContext
    {
        public class AttendanceDbContext : DbContext
        {
            //public AttendanceDbContext(DbContextOptions<AttendanceDbContext> options)
            //    : base(options)
            //{
    
            //}
            public DbSet<CustomerModel> Customer { get; set; }
            
        }
    }
    

    Friday, July 26, 2019 1:56 PM

All replies