WNM Derek
WNM Derek

Reputation: 55

AddDbContext not working in ConfigureServices of ASP.NET Core Razor Pages

I reversed a SQL server database into their respective classes and context, and all this appears to of worked fine.

In the min context class, there is a line of code pointing to the connection string, which if left in works fine, but the connection string is fully embedded in the context file.

I tried commenting it out of the context and adding via the ConfigureServices section (so I can use the conn string in appsettings.json) using

services.AddDbContext<myDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("dbconn")));

But when I run, I get an error:-

System.InvalidOperationException: 'No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.'

I have tried numerous things on Google but I always end up with the same error, unless I uncomment the hard-coded connection string in the context class file.

Any idea what I am doing wrong?

I have tried many many Google responses and all have the same error - the:-

System.InvalidOperationException: 'No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.'

I comment this out from the context class:-

optionsBuilder.UseSqlServer("Server=MYSERVER\\MSSQL2017EXPRESS;Database=DBNAME;Trusted_Connection=True;user id=DBUSER;password=DBPASSWORD;");

And this is what is in startup.cs but not working:-

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<CookiePolicyOptions>(options =>
    {
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.None;
    });
   services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

    //My DB conn
    services.AddDbContext<myDatabaseContext>(options => options.UseSqlServer(Configuration.GetConnectionString("dbconn")));

}

I need to be able to provide the connection string from appsettings.json and not have it hard coded in to the context class file.

Upvotes: 2

Views: 9663

Answers (2)

FreeBird
FreeBird

Reputation: 249

You are most likely missing the package Microsoft.EntityFrameworkCore.SqlServer.

Upvotes: -1

Nkosi
Nkosi

Reputation: 247471

You are most likely missing a constructor on your context.

public myDatabaseContext(DbContextOptions<myDatabaseContext> options) : base(options) {
    //...
}

as advised in the error message

If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.'

Upvotes: 3

Related Questions