简体   繁体   中英

System.InvalidOperationException: No database provider has been configured for this DbContext

I added entity model and context but I get error shown below.

Can you please help me with this?

public class SiteDbContext : DbContext
{  private readonly DatabaseSettings _databaseSettings;
public SiteDbContext()
{                 }
public DbSet
<Admission>
Admission { get; set; }          
protected override void OnModelCreating(ModelBuilder modelBuilder)
{         base.OnModelCreating(modelBuilder);       }    }`

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

If you have register dbContext like below

services.AddDbContext<SiteDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));

Then you need to modify your DbContext constructor like:

public class SiteDbContext: DbContext
{
    public SiteDbContext(DbContextOptions<SiteDbContext> options): base (options)
    {
    }
    //...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM