Ryan Thom
Ryan Thom

Reputation: 25

Creating an ApplicationDbContext Object

When I try to use base("DefaultConnection") I get an error that it "cannot convert from 'string' to 'microsoft.entityframeworkcore.dbcontextoptions'"

public ApplicationDbContext() : base("DefaultConnection")
{
}

Can I remove the base? I tried but then it gave me an error when I tried to access my database, it gave me an error about my constructor.

Upvotes: 1

Views: 256

Answers (1)

Vivek Nuna
Vivek Nuna

Reputation: 1

You are doing in wrong way, it must be declared like this.

public ApplicaionDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }

And please make sure you register the ApplicationDbContext like the below.

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));

    ...
}

Upvotes: 2

Related Questions