【问题标题】:'DbContextOptionsBuilder' does not contain a definition for 'UseSqlServer'“DbContextOptionsBuilder”不包含“UseSqlServer”的定义
【发布时间】:2017-03-13 12:48:27
【问题描述】:

我正在尝试使用 C# 在 VS 2015 Pro(更新 3)中创建一个 Web API,并以 .NET Core 为目标。

我关注this tutorial。 但是,我连接的是 MySQL 数据库而不是 SQL Server 数据库 - 不知道这有多大区别...

无论如何,在本教程中,我必须“使用依赖注入注册我的上下文” - 所以我必须将以下行添加到 ConfigureServices 部分的 strong>Startup.cs 文件:

var connection = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext< Models.PropWorxContext > (options => options.UseSqlServer(connection));;

但是,VS 给了我以下错误:

错误 CS1061“DbContextOptionsBuilder”不包含“UseSqlServer”的定义,并且找不到接受“DbContextOptionsBuilder”类型的第一个参数的扩展方法“UseSqlServer”(您是否缺少 using 指令或程序集引用?)

有什么想法吗?

这是整个 Startup.cs 文件的样子:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace PropWorxAPI
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; }

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

            var connection = Configuration.GetConnectionString("DefaultConnection");
            services.AddDbContext< Models.PropWorxContext > (options => options.UseSqlServer(connection));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseMvc();
        }
    }
}

我还使用包管理器添加了 MySQL 包,因此我的 project.json 文件包含以下条目:

*"MySql.Data.EntityFrameworkCore": "7.0.6-IR31"*

任何关于我哪里出错的提示将不胜感激,因为我花了一整天的时间试图弄清楚:(谢谢...

【问题讨论】:

    标签: mysql asp.net-mvc entity-framework asp.net-web-api asp.net-core


    【解决方案1】:

    缺少包裹。这对我有用。 工具-NUget包-包管理器-复制粘贴以下安装:

    安装包 Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.1

    【讨论】:

      【解决方案2】:

      安装 Sqllite/Sqlserver 紧凑工具箱,然后创建数据库,如果未创建则更改 appsettings.json 中的连接字符串

      【讨论】:

        【解决方案3】:

        在您的项目文件夹中,在命令行中输入:

        dotnet add package Pomelo.EntityFrameworkCore.MySql -v 2.1.2
        

        【讨论】:

          【解决方案4】:

          如果您已经安装了“Microsoft.EntityFrameworkCore.SqlServer”包并且使用 Ctrl + Space 无法为您提供任何选项,则手动添加“使用 Microsoft.EntityFrameworkCore”解决了我的问题。

          【讨论】:

            【解决方案5】:

            我在使用 Sql Server 为数据库设置项目时也遇到了这个错误。在我的情况下,我必须手动添加一个 using 语句 - “使用 Microsoft.EntityFrameworkCore;” .这听起来有点明显,但它让我感到震惊,因为 Visual Studio 没有通过快速操作添加 using 语句。

            【讨论】:

              【解决方案6】:

              SqlServer 是 Microsoft Sql Server 而不是 MySql,要使用 SqlServer,您需要包“Microsoft.EntityFrameworkCore.SqlServer”:“1.0.*”。

              如果使用 MySql 有 options.UseMySql

              看到这个tutorial for more

              【讨论】:

                猜你喜欢
                • 2017-11-25
                • 2021-08-03
                • 2021-12-17
                • 2017-08-05
                • 1970-01-01
                • 2020-06-17
                • 2017-08-23
                • 1970-01-01
                相关资源
                最近更新 更多