Click here to Skip to main content
15,909,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Unable to create a new controller.

What I have tried:

EmplyeeContext.cs

C#
using Microsoft.EntityFrameworkCore;

namespace WebApi.Model
{
    public class EmployeeContext : DbContext // Inherit
    {
        // Constructor
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
        public EmployeeContext(DbContextOptions<employeecontext> options) : base(options) // DI
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
        {
            
        }

        // DataSet Property of the Classes
        public DbSet<tblemployee> TblEmployee { get; set; }
        public DbSet<tbldesignation> TblDesignation { get; set; }

    }
}



Startup.cs

C#
using Microsoft.EntityFrameworkCore;
using WebApi.Model;

namespace WebApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public IConfiguration Configuration { get; }
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddDbContext<employeecontext>(db => db.UseSqlServer(Configuration.GetConnectionString("ConEmp")));
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if(env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseHttpsRedirection();
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
Posted
Updated 29-Jun-22 4:25am
v3

1 solution

Please check name in constructor is not same as class name.

public EmployeeContext(DbContextOptions<employeecontext> options)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900