“DbContextOptionsBuilder”不包含“UseSqlServer”的定义。

12
我正在尝试使用C#创建一个Web API,目标是.NET Core,使用的是VS 2015 Pro(Update 3)。我遵循本教程。但是,我连接的是MySQL数据库,而不是SQL Server数据库-不确定这是否有多大区别...无论如何,在教程中,我必须“使用依赖注入注册我的上下文”,因此我必须将以下行添加到Startup.cs文件的ConfigureServices部分:
var connection = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext< Models.PropWorxContext > (options => options.UseSqlServer(connection));;

然而,VS 给了我以下错误:Error CS1061 'DbContextOptionsBuilder' does not contain a definition for 'UseSqlServer' and no extension method 'UseSqlServer' accepting a first argument of type 'DbContextOptionsBuilder' could be found (are you missing a using directive or an assembly reference?)。有任何想法吗?这是整个 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"*

如果您能提供任何线索,帮助我找出问题所在,将不胜感激。我已经花了一整天的时间来尝试解决它:(。谢谢...

6个回答

41

SqlServer是微软的Sql Server,不是MySql。要使用SqlServer,您需要安装包“Microsoft.EntityFrameworkCore.SqlServer”:“1.0.*”。

如果使用MySql,则有options.UseMySql选项。

欲了解更多信息,请参阅此教程


15

在我使用Sql Server作为数据库设置项目时,我也遇到了这个错误。在我的情况下,我需要手动添加一个using语句——"using Microsoft.EntityFrameworkCore;"。听起来有点显而易见,但是这让我困惑了,因为Visual Studio没有通过快速操作添加using语句。


9
如果您已经安装了“Microsoft.EntityFrameworkCore.SqlServer”包,但使用Ctrl + Space无法给您任何选项,则手动添加“using Microsoft.EntityFrameworkCore”可以解决我的问题。

5

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

Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.1


1
在您的项目文件夹中,键入以下内容到命令行中:
dotnet add package Pomelo.EntityFrameworkCore.MySql -v 2.1.2

-1

安装Sqllite/Sqlserver Compact Toolbox,如果没有创建数据库,则创建数据库,然后在appsettings.json中更改连接字符串。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接