MySQL “DbContextOptionsBuilder”不包含“UseSqlServer”的定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40342137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'DbContextOptionsBuilder' does not contain a definition for 'UseSqlServer'
提问by Fabricio Rodriguez
I am trying to create a Web API in VS 2015 Pro (Update 3)?using C# and targeting .NET Core.
我正在尝试在 VS 2015 Pro(更新 3)中创建一个 Web API?使用 C# 并针对 .NET Core。
I am following this tutorial: https://docs.efproject.net/en/latest/platforms/aspnetcore/new-db.html#install-entity-framework. However, I am connecting to a MySQL database instead of an SQL Server database - not sure how much difference that makes...
我正在关注本教程:https: //docs.efproject.net/en/latest/platforms/aspnetcore/new-db.html#install-entity-framework。但是,我连接的是 MySQL 数据库而不是 SQL Server 数据库-不确定这有多大区别...
Anyway, in the tutorial, I have to "Register?my context with dependency injection"- so I have to add the following line to the ConfigureServicessection of the Startup.csfile:
无论如何,在本教程中,我必须“注册?我的依赖注入上下文”- 所以我必须将以下行添加到Startup.cs文件的ConfigureServices部分:
var connection = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext< Models.PropWorxContext > (options => options.UseSqlServer(connection));;
However, VS gives me the following error:
但是,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?)
错误?CS1061?“DbContextOptionsBuilder”不包含“UseSqlServer”的定义,并且找不到接受“DbContextOptionsBuilder”类型的第一个参数的扩展方法“UseSqlServer”(您是否缺少 using 指令或程序集引用?)
Any ideas why?
任何想法为什么?
This is what the whole Startup.cs file looks like:
这是整个 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();
}
}
}
I have also?added the MySQL package using the Package Manager, so that my project.jsonfile contains this entry:
我还使用包管理器添加了 MySQL 包,以便我的project.json文件包含以下条目:
*"MySql.Data.EntityFrameworkCore":?"7.0.6-IR31"*
Any hints as to where I've gone wrong would be greatly appreciated, as I've spent all day trying to figure it out :( Thank you...
任何关于我哪里出错的提示将不胜感激,因为我花了一整天的时间试图弄清楚:(谢谢...
回答by Joe Audette
SqlServer is Microsoft Sql Server not MySql, to use SqlServer you would need the package "Microsoft.EntityFrameworkCore.SqlServer": "1.0.*".
SqlServer 是 Microsoft Sql Server 而不是 MySql,要使用 SqlServer,您需要包“Microsoft.EntityFrameworkCore.SqlServer”:“1.0.*”。
If using MySql there is options.UseMySql
如果使用 MySql 有 options.UseMySql
see this tutorial for more
请参阅本教程了解更多
回答by Frank Cannon
I got this error as well whilst setting up a project using Sql Server for a database. In my case I had to add a using statement manually - "using Microsoft.EntityFrameworkCore;" . It sounds a bit obvious but it threw me as Visual Studio wasn't adding the using statement via Quick Actions.
我在使用 Sql Server 为数据库设置项目时也遇到了这个错误。就我而言,我必须手动添加 using 语句 - “使用 Microsoft.EntityFrameworkCore;” . 这听起来有点明显,但它让我失望,因为 Visual Studio 没有通过快速操作添加 using 语句。
回答by HoganDevelopementAddicted
If you have already installed the "Microsoft.EntityFrameworkCore.SqlServer" package and using Ctrl + Space cannot give you any option, adding the "using Microsoft.EntityFrameworkCore" manually solved my issue.
如果您已经安装了“Microsoft.EntityFrameworkCore.SqlServer”包并且使用 Ctrl + Space 无法为您提供任何选项,则手动添加“使用 Microsoft.EntityFrameworkCore”解决了我的问题。
回答by user10930337
While in your project folder type this into your command line:
在您的项目文件夹中,在命令行中输入以下内容:
dotnet add package Pomelo.EntityFrameworkCore.MySql -v 2.1.2
回答by Moaz Salem
Install Sqllite/Sqlserver compact toolbox then create database if not created then change connectionstring in appsettings.json
安装 Sqllite/Sqlserver 紧凑型工具箱,然后创建数据库(如果未创建)然后更改 appsettings.json 中的连接字符串
回答by Bnezes
missing package.This worked for me. tools-NUget package-package manager-copy paste following install:
缺少包。这对我有用。工具-NUget 包-包管理器-在安装后复制粘贴:
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.1
安装包 Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.1