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

.net-core c# dbcontext entity-framework-core linq

Question

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.

1
2
7/16/2018 6:52:41 AM

Accepted Answer

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.

12
4/6/2017 10:25:51 PM

Popular Answer

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.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow