繁体   English   中英

InvalidOperationException:没有为此DbContext配置数据库提供程序。

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

使用EntityFrameworkCore 2.03 .NET Core构建Web API

不断收到以下错误,尝试了我能想到的一切,以解决问题并不确定其他人是否遇到了这个问题?

InvalidOperationException:没有为此DbContext配置数据库提供程序。 可以通过重写DbContext.OnConfiguring方法或在应用程序服务提供程序上使用AddDbContext来配置提供程序。 如果使用AddDbContext,则还请确保您的DbContext类型在其构造函数中接受DbContextOptions对象,并将其传递给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)
        {
        }
    }
}

这是您最根本的问题:

public Repository()
{
    _context = new ChurchContext();
}

这与依赖注入相反。 这方面您手动创建尚未配置。

快速回答:

public Repository(ChurchContext context)
{
    _context = context;
}

此外:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM