2015-10-11 107 views
21

我想实现实体框架7 MVC 6,而这个页面here它说做UseSqlServer方法缺少MVC 6

services.AddEntityFramework() 
    .AddSqlServer() 
    .AddDbContext<MusicStoreContext>(options => 
         options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])); 

但对我时,UseSqlServer方法心不是可见?任何人都知道如何使其可见?或者这是一种配置实体框架的旧方法?

我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<DataContext>(); 

     } 
    } 
} 

回答

20

UseSqlServer是在命名空间的扩展方法Microsoft.Data.Entity,所以你需要导入你的代码,就像这样:

using Microsoft.Data.Entity; 

编辑: 此建议现在已过时(我无法删除它,因为它是被接受的答案)。命名空间已经改变,您现在应该使用:

using Microsoft.EntityFrameworkCore; 
+0

谢谢你,这是正是我需要的 – Gillardo

+0

完美的作品......现在得到了另一个问题说“没有配置数据库提供者。在设置服务时,通过在DbContext类或AddDbContext方法中重写OnConfiguring来配置数据库提供程序。“不知道为什么,因为我的startup.cs文件说明了conectionstring? – Gillardo

+0

声音很容易修复,但这是另一个问题:) – DavidG

26

由于已发布,程序集已重命名。作为EntityFrameworkCore的一部分,您现在需要添加一个using语句下面

using Microsoft.EntityFrameworkCore; 

而且.UseSqlServer扩展方法来配置方面将变得可用

27

安装Microsoft.EntityFrameworkCore.SqlServer 1.0.1包装工程我 Microsoft.EntityFrameworkCore的版本是1.1.0

+0

是的,这是最新的,谢谢 –

+0

这是截至2017年3月24日的正确答案NuGet:[Microsoft.EntityFrameworkCore.SqlServer](https://www.nuget。 org/packages/Microsoft.EntityFrameworkCore.SqlServer /)。 – Win

+0

像一个魅力安装包Microsoft.EntityFrameworkCore.SqlServer – DaImTo

3

这是一个的NuGet软件包问题

安装以下软件包,并影响其正常版本

  1. Microsoft.EntityFrameworkCore(最新版)
  2. Microsoft.EntityFrameworkCore.SqlServer(1.0.4版)