No database provider has been configured for this DbContext for get method in Asp.netCore

asp.net-core asp.net-web-api c# entity-framework-core

Question

I create an web api project by Asp.net core, and i added an api controller to it(by the name BlogController), in blog controller i have a get method GetAllBlog this is my controller:

[Route("api/[controller]")]
public class BlogController : Controller
{
    private static Logger logger = LogManager.GetCurrentClassLogger();

    public IContext _context { get; set; }
    public BlogController(IContext ctx)
    {
        _context = ctx;
    }

    [HttpGet]
    public IEnumerable<Blog> GetAllBlog()
    {
        return _context.Blogs.ToList();
    }
 }

this is my IContext and model:

public interface IContext : IDisposable
{
    DbSet<Blog> Blogs { get; set; }
    DbSet<Post> Posts { get; set; }
    int SaveChanges();
}

and context:

public class Context : DbContext, IContext
{
    public Context(DbContextOptions<Context> options) : base(options)
    { }
    public virtual DbSet<Blog> Blogs { get; set; }
    public virtual DbSet<Post> Posts { get; set; }
}

and model:

public class Blog
{
    public int BlogId { get; set; }
    public string Name { get; set; }
    public string Url { get; set; }
    public DateTime? CreationDate { get; set; }
    public virtual IList<Post> Posts { get; set; }
}

when i call GetAllBlog() i got this error:

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. what is the problem?

UPDATE: this is the configurationservice method in Startup class:

public void ConfigureServices(IServiceCollection services)
{
    var connection = @"Data Source=.;Initial Catalog=RestfullServices;Integrated Security=true";
    services.AddDbContext<Context>(options => options.UseSqlServer(connection));
    services.AddScoped<IContext>(p => new Context(new DbContextOptions<Context>()));
    services.AddApplicationInsightsTelemetry(Configuration);
    services.AddMvc();
}
1
2
1/1/2017 12:33:52 PM

Accepted Answer

When configuring the DbContext

services.AddDbContext<Context>(options => options.UseSqlServer(connection));

you configure it to use specific options options.UseSqlServer(connection)

but when configuring the scoped context abstraction

services.AddScoped<IContext>(p => new Context(new DbContextOptions<Context>()));

a new Context is being created with a completely different configuration to what was configured before.

by changing how the IContext is registered with the DI framework during startup like this

services.AddScoped<IContext, Context>();

The DI framework will use the AddDbContext configuration when creating instances of Context instead, which will have the options you want to use from startup configuration when creating instances of the DbContext.

the Startup.ConfigurServices would end up looking like this...

public void ConfigureServices(IServiceCollection services) {
    var connection = @"Data Source=.;Initial Catalog=RestfullServices;Integrated Security=true";
    services.AddDbContext<Context>(options => options.UseSqlServer(connection));
    services.AddScoped<IContext, Context>();
    services.AddApplicationInsightsTelemetry(Configuration);
    services.AddMvc();
}
2
1/2/2017 2:41:21 PM


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