简体   繁体   中英

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)
        {
        }
    }
}

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:

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