Cannot create a object of Type 'ReactJsContext' In web Api

Viewed 17

My problem is when i run the migration command dotnet ef migration add --project pname i get this error

Unable to Create a object of type "Dbcontext"

i follow all solution that i found on google but this error does not fix.I doesnt find any solution to

My Dbcontext

namespace ReactJsNew.Data
{
    public class ReactJsContext : DbContext
    {
        public ReactJsContext(DbContextOptions<ReactJsContext> options)
               : base(options)
        {
        }

        public DbSet<Login> Logins { get; set; }
        public DbSet<Register> Registers { get; set; }
    }

}

This is my Program File

namespace ReactJsNew
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
}

This is my startup file

namespace ReactJsNew
{
    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.AddControllers();
            services.AddSignalR();

            services.AddCors(options =>
            {
                services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
           .AddJwtBearer(options => {
                    //options.SaveToken = true;
                    options.TokenValidationParameters = new TokenValidationParameters
               {
                   ValidateIssuer = true,
                   ValidateAudience = true,
                   ValidateLifetime = true,
                   ValidateIssuerSigningKey = true,
                   ValidIssuer = Configuration["Jwt:Issuer"],
                   ValidAudience = Configuration["Jwt:Audience"],
                   IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"]))
               };
           });
                services.AddMvc();
                services.AddControllersWithViews();
                services.AddDbContext<ReactJsContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("ReactJsNew")));
                options.AddPolicy("ClientPermission", policy =>
                {
                    policy.AllowAnyHeader()
                        .AllowAnyMethod()
                        .WithOrigins("http://localhost:3000")
                        .AllowCredentials();
                });
            });
        }

        // 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();
            }

            app.UseHttpsRedirection();

            app.UseCors("ClientPermission");

            app.UseRouting();

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

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapHub<ChatHub>("/hubs/chat");
            });
        }
    }
}
1 Answers

You can try to use below code. It should works for you.

namespace ReactJsNew.Data
{
    public class ReactJsContext : DbContext
    {
        // add this
        public ReactJsContext (){
        }

        public ReactJsContext(DbContextOptions<ReactJsContext> options)
               : base(options)
        {
        }

        public DbSet<Login> Logins { get; set; }
        public DbSet<Register> Registers { get; set; }
    }

}
Related