简体   繁体   中英

DotNet Core , No database provider has been configured for this DbContext

I want to select data from my table "Header" using a simple LINQ command but I faced error.

My Action

    public HeaderModel GetHeaderInformation()
    {
        using(var context = new ApplicationDbContext())
        {
            var header = context.Headers.Select(x => new HeaderModel
            {
                colorCode = x.colorCode,
                height = x.height,
                Id = x.Id,
                left = x.left,
                top = x.top,
                width = x.width
            }).FirstOrDefault();

            return header;
        }
    }

The Error

Additional information: 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 object in its constructor and passes it to the base constructor for DbContext.

My ApplicationDbContext

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options) { }

    public ApplicationDbContext() : base() { }

    public DbSet<Header> Headers { get; set; }
    public DbSet<Menu> Menus { get; set; }
}

My Startup.cs

        services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddApplicationInsightsTelemetry(Configuration);
        services.AddIdentity<ApplicationUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
        services.AddMvc();

Thanks in advance.

You are using dependency injection. services.AddDbContext is in charge of creating you an DbContext object. There is no point in having that using block because, by doing that, you are instancing a new ApplicationDbContext that doesn't have a connection string.


Write your method like this:

public HeaderModel GetHeaderInformation(ApplicationDbContext context)
{
    // the code inside your using block
}

and .Net will resolve the context via dependency injection.



Furthermore, a common practice is to have the DbContext as a private readonly atribute in your constructor class. So you may want do something like this:

public class MyConroller : Controller
{
    private readonly MyDbContext _context;

    public MyConroller(MyDbContext ctx)
    {
        _context = ctx;
    }
}

and just use the context atribute in your methods.

You should remove your parameterless constructor, because it is probably the one that is being called when a new instance is created by the DI. I know that for some people this was the problem, I hope it helps.

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