2
votes

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
You should also show the configuration where you add the db contextNkosi
How and where you map IContext with your IoC? can you post it?federico scamuzzi
has to do with the new options you create when adding the IContext to IoCNkosi
@Nkosi what is ur mean? can u explain me?pejman
ok before I do lets try something. try services.AddScoped<IContext, Context>(); and see if that works.Nkosi

1 Answers

2
votes

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();
}