I got the error when initialize the dbModel context as follows:
This is my dbContext class:
public class DbModel : DbContext
{
public DbModel()
{
}
public DbModel(DbContextOptions<DbModel> options)
: base(options)
{ }
public DbSet<UserModel> User {get;set;}
}
I added the following code in the ConfigureServices method in Startup.cs according to some answers in stackoverflow, but I'm still getting an error.
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
This is my ConfigureServices method:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
var connection = @"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.AspNetCore.Restaurant;Trusted_Connection=True;ConnectRetryCount=0";
services.AddDbContext<DbModel>(options => options.UseSqlServer(connection));
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "Restaurant APIs", Description = "Swagger Core API" });
});
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
}
What could be the reason?
