'DbContextOptionsBuilder'不包含'UseSqlServer'的定义 [英] 'DbContextOptionsBuilder' does not contain a definition for 'UseSqlServer'
问题描述
我正在尝试使用C#和目标.NET Core在VS 2015 Pro(Update 3)中创建Web API.
I am trying to create a Web API in VS 2015 Pro (Update 3) using C# and targeting .NET Core.
我正在遵循本教程: https://docs.efproject.net/en/latest/platforms/aspnetcore/new-db.html#install-entity-framework . 但是,我正在连接到MySQL数据库而不是SQL Server数据库-不知道有什么区别...
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...
无论如何,在本教程中,我必须通过依赖项注入注册我的上下文" -因此,我必须将以下行添加到 Startup.cs 文件:
Anyway, in the tutorial, I have to "Register my context with dependency injection" - so I have to add the following line to the ConfigureServices section of the Startup.cs file:
var connection = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext< Models.PropWorxContext > (options => options.UseSqlServer(connection));;
但是,VS给我以下错误:
However, VS gives me the following error:
错误CS1061'DbContextOptionsBuilder'不包含'UseSqlServer'的定义,并且找不到扩展方法'UseSqlServer'接受类型为'DbContextOptionsBuilder'的第一个参数(是否缺少using指令或程序集引用?)
有什么想法吗?
这是整个Startup.cs文件的样子:
This is what the whole Startup.cs file looks like:
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 文件包含以下条目:
I have also added the MySQL package using the Package Manager, so that my project.json file contains this entry:
*"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...
推荐答案
SqlServer是Microsoft Sql Server,而不是MySql,要使用SqlServer,您需要包"Microsoft.EntityFrameworkCore.SqlServer":"1.0.*".
SqlServer is Microsoft Sql Server not MySql, to use SqlServer you would need the package "Microsoft.EntityFrameworkCore.SqlServer": "1.0.*".
如果使用MySql,则有选项.UseMySql
If using MySql there is options.UseMySql
请参见 查看全文