我正在尝试使用C#创建一个Web API,目标是.NET Core,使用的是VS 2015 Pro(Update 3)。我遵循本教程。但是,我连接的是MySQL数据库,而不是SQL Server数据库-不确定这是否有多大区别...无论如何,在教程中,我必须“使用依赖注入注册我的上下文”,因此我必须将以下行添加到Startup.cs文件的ConfigureServices部分:
然而,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 文件的样子:
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"*
如果您能提供任何线索,帮助我找出问题所在,将不胜感激。我已经花了一整天的时间来尝试解决它:(。谢谢...