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

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



build a web api using entityframeworkcore 2.03 .net core



keep getting the following error tried everything i can think of to get past it not sure what the issue is has anyone else had this iasue ?



InvalidOperationException: 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



startup.cs


using churchy.Repository;
using churchy.Service;
using churchy.Service.Abstractions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace churchy

public class Startup

public Startup(IConfiguration configuration)

Configuration = configuration;


public IConfiguration Configuration get;

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)


// Database connection
services.AddDbContext<ChurchContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("ChurchConnection")));

// Repositories
services.AddScoped<IRepository, Repository.Repository>();
services.AddScoped<IRepositoryFactory, RepositoryFactory>();

// Services
services.AddScoped<IChurchService, ChurchService>();

services.AddMvc();


// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)

if (env.IsDevelopment())

app.UseDeveloperExceptionPage();


app.UseMvc();





churchcontext.cs


using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EntityFrameworkCore;
using churchy.Model;


namespace churchy.Repository

public class ChurchContext: DbContext

public ChurchContext()



public ChurchContext(DbContextOptions<ChurchContext> options) : base(options)




public DbSet<Church> Churches get; set;
public DbSet<Location> Locations get; set;

protected override void OnModelCreating(ModelBuilder modelBuilder)

modelBuilder.Entity<Church>().ToTable("Church");





Repository.cs


using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading.Tasks;

namespace churchy.Repository

public class Repository : IRepository

private readonly ChurchContext _context;

public Repository()

_context = new ChurchContext();


public IQueryable<T> GetAll<T>() where T : class

return _context.Set<T>();


public Task Create<T>(T entity) where T : class

throw new System.NotImplementedException();


public Task Delete<T>(int id) where T : class

throw new System.NotImplementedException();


public Task Update<T>(int id, T entity) where T : class

throw new System.NotImplementedException();


public void Dispose()

_context?.Dispose();





IRepository.cs


using System;
using System.Linq;
using System.Threading.Tasks;

namespace churchy.Repository

public interface IRepository : IDisposable

IQueryable<T> GetAll<T>() where T : class;
Task Create<T>(T entity) where T :class;
Task Update<T>(int id, T entity) where T : class;
Task Delete<T>(int id) where T : class;




ChurchController


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using churchy.Service.Abstractions;

namespace churchy.Controllers

[Route("api/church")]
public class ChurchController : Controller

private readonly IChurchService _churchService;
public ChurchController(IChurchService churchService)

_churchService = churchService;

// GET: api/<controller>
[HttpGet]
public IActionResult GetAllAsync()

var response = _churchService.getChurches();

return Ok(response);


// GET api/<controller>/5
[HttpGet("id")]
public string Get(int id)

return "value3";


// POST api/<controller>
[HttpPost]
public void Post([FromBody]string value)



// PUT api/<controller>/5
[HttpPut("id")]
public void Put(int id, [FromBody]string value)



// DELETE api/<controller>/5
[HttpDelete("id")]
public void Delete(int id)








When debugging, which constructor gets called in your ChurchContext class?
– Valuator
May 25 at 21:28


ChurchContext





If you want that IRepository will be instantiated automatically by dependency injection container add context as parameter to constructor of Repository. public Repository(ChurchContext context)
– Fabio
May 25 at 21:31



IRepository


public Repository(ChurchContext context)





Your controller depends on IChurchService, but you didn't show it implementation. If service depend on repository it should be introduced as constructor parameter.
– Fabio
May 25 at 21:32


IChurchService




1 Answer
1



Here is your most fundamental problem:


public Repository()

_context = new ChurchContext();



That's the opposite of Dependency Injection. That context you are manually creating has not been configured.



Quick answer:


public Repository(ChurchContext context)

_context = context;



Furthermore:





thanks for your reply that line i did change to add the D Ppublic Repository() _context = new ChurchContext();
– user2286483
May 25 at 22:47






made that change but getting this error now There is no argument given that corresponds to the required formal parameter 'context' of 'Repository.Repository(ChurchContext)' x ` namespace churchy.Repository public class RepositoryFactory : IRepositoryFactory public IRepository CreateRepository() return new Repository(); `
– user2286483
May 26 at 11:17



There is no argument given that corresponds to the required formal parameter 'context' of 'Repository.Repository(ChurchContext)'





@user2286483 That class shouldn't exist, the DI Container creates instances for you, you don't need it
– Camilo Terevinto
May 26 at 11:39



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế