【问题标题】:getting error while inserting data in the database using ef core , No database provider has been configured for this DbContext使用 ef core 在数据库中插入数据时出错,没有为此 DbContext 配置数据库提供程序
【发布时间】:2019-10-09 22:13:27
【问题描述】:

我正在使用 if core 将数据插入到 .net core 中的数据库中,我能够从 get API 获取数据,但是当我将数据插入到数据库中时出现错误。并且它在 Context 出现编译错误。

错误 InvalidOperationException:没有为此 DbContext 配置数据库提供程序。可以通过覆盖 DbContext 来配置提供程序。在配置方法上或通过在应用程序服务提供者上使用 AddDbContext。如果使用了 AddDbContext,那么还要确保您的 DbContext 类型在其构造函数中接受 DbContextOptions 对象并将其传递给 DbContext 的基本构造函数。

上下文类

 public class Context : DbContext
    {
        public Context(DbContextOptions<Context> options) : base(options)
        {

        }
        public DbSet<Mplus> MpData { get; set; }

    }

API控制器

namespace MplusApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MPlusController : ControllerBase
    {

        private Context _context;

        public MPlusController(Context context) {
            _context = context;
        }

        [HttpGet("{Ftemp}")]
        public IActionResult Get(int Ftemp) {

            using (var context = new Context())//Getting compilation error here
            {
                var mp = new Mplus()
                {
                    Ftemp = Ftemp,

                };
                context.MpData.Add(mp);
                context.SaveChanges();
            }
            return Ok();
        }

    }

型号

 public class Mplus
    {

        public int Ftemp { get; set; }
    }

startup.cs

 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)
        {
            services.AddDbContext<Context>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

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

【问题讨论】:

    标签: .net model-view-controller entity-framework-core


    【解决方案1】:

    您需要告诉 EF 使用哪个提供程序(SQL Server、SQLite 等)。你有两个选择。在 Startup 类的 ConfigureServices 方法上注册 DbContext 时这样做,如下所示:

    services.AddDbContext<Context>(options => options.UseSqlServer(@"connection string"));
    

    或者在 DbContext 派生类的 OnConfiguring 方法上执行此操作:

    protected override void OnConfiguring(DbContextOptionsBuilder builder)
    {
        builder.UseSqlServer(@"connection string");
    

     }

    https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext

    此外,您需要使用正在注入的 Context 实例,而不是构建新的 Context 对象 - 如果您这样做,您需要传递一个 DbContextOptions 实例,因为您没有公共无参数构造函数。

    类似这样的:

    var mp = new Mplus()
    {
        Ftemp = Ftemp,
    };
    _context.MpData.Add(mp);
    _context.SaveChanges();
    

    【讨论】:

    • 我需要写哪个文件。启动.cs?
    • 是的,ConfigureServices 方法。
    • 我已经写过了。你能看看我更新的帖子吗?
    • 你需要使用注入的构造函数,_context,而不是新建一个。请参阅我的更新答案。
    • 来吧,这是一个很基础的问题……你必须使用_context,而不是创建一个新的实例(去掉using块)。
    猜你喜欢
    • 2020-06-16
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 2021-05-04
    相关资源
    最近更新 更多