【发布时间】: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