UseSqlServer方法缺少MVC 6
我正在尝试在MVC 6中实现Entity Framework 7,并在此页面上说它要做
services.AddEntityFramework() .AddSqlServer() .AddDbContext(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
但对我来说, UseSqlServer
方法不可见? 有谁知道如何让它可见? 或者这是配置entity framework的旧方法?
我的startup.cs文件看起来像这样
using FluentValidation; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Hosting; using Microsoft.Framework.ConfigurationModel; using Microsoft.Framework.DependencyInjection; namespace me.namespace.project { public class Startup { public static IConfiguration Configuration { get; set; } public Startup(IHostingEnvironment env) { // Setup configuration sources. Configuration = new Configuration() .AddJsonFile("config.json") .AddEnvironmentVariables(); } public void ConfigureServices(IServiceCollection services) { services.AddMvc(); // entity framework services.AddEntityFramework() .AddSqlServer() .AddDbContext(); } } }
UseSqlServer
是名称空间Microsoft.Data.Entity
的扩展方法,因此您需要在代码中导入它,如下所示:
using Microsoft.Data.Entity;
编辑:这个建议现在已经过时了(由于它是被接受的答案,我无法删除它)。 命名空间已经改变,您现在应该使用:
using Microsoft.EntityFrameworkCore;
安装Microsoft.EntityFrameworkCore.SqlServer 1.0.1包适用于我.Microsoft.EntityFrameworkCore的版本是1.1.0
由于已发布,因此已重命名程序集。 作为EntityFrameworkCore的一部分,您现在需要添加以下的using语句
using Microsoft.EntityFrameworkCore;
并且用于配置上下文的.UseSqlServer扩展方法将可用
这是一个NuGet包问题
安装以下软件包及其正确版本
- Microsoft.EntityFrameworkCore(最新版本)
- Microsoft.EntityFrameworkCore.SqlServer(1.0.4版)