'DbContextOptionsBuilder'不包含'UseSqlServer'的定义

Fab*_*uez 5 mysql asp.net-mvc entity-framework asp.net-web-api asp.net-core

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

我正在学习本教程:https://docs.efproject.net/en/latest/platforms/aspnetcore/new-db.html#install-entity-framework.但是,我连接的是MySQL数据库而不是SQL Server数据库 - 不确定它有多大区别......

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

var connection = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext< Models.PropWorxContext > (options => options.UseSqlServer(connection));;
Run Code Online (Sandbox Code Playgroud)

但是,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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

*"MySql.Data.EntityFrameworkCore": "7.0.6-IR31"*
Run Code Online (Sandbox Code Playgroud)

任何关于我出错的提示都会非常感激,因为我花了一整天时间试图解决这个问题:(谢谢......

Joe*_*tte 21

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

如果使用MySql,则有options.UseMySql

请参阅本教程了解更多信息


Fra*_*non 12

我在使用Sql Server为数据库设置项目时也遇到了这个错误.在我的情况下,我不得不手动添加一个using语句 - "使用Microsoft.EntityFrameworkCore;" .这听起来有点明显,但它让我觉得因为Visual Studio没有通过Quick Actions添加using语句.


小智 8

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


小智 5

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

安装包 Microsoft.EntityFrameworkCore.SqlServer -版本 3.1.1