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

时间:2018-05-25 21:23:58

标签: c# .net entity-framework asp.net-core-2.0 asp.net-core-webapi

使用entityframeworkcore 2.03 .net core

构建web api

继续得到以下错误尝试了我能想到的一切,以便不知道问题是什么,其他人有这个iasue?

  

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

1 个答案:

答案 0 :(得分:4)

这是你最基本的问题:

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

这与依赖注入相反。您手动创建的上下文已配置

快速回答:

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

此外: