UseSqlServer方法缺少MVC 6

时间:2015-10-11 10:57:19

标签: c# asp.net .net asp.net-core-mvc entity-framework-core

我正在尝试在MVC 6中实现Entity Framework 7,并在此页here上表示要执行

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

        }
    }
}

4 个答案:

答案 0 :(得分:37)

安装Microsoft.EntityFrameworkCore.SqlServer 1.0.1包对我有用 Microsoft.EntityFrameworkCore的版本是1.1.0

答案 1 :(得分:32)

B是名称空间UseSqlServer中的扩展方法,因此您需要在代码中导入它,如下所示:

Microsoft.Data.Entity

答案 2 :(得分:28)

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

using Microsoft.EntityFrameworkCore;

用于配置上下文的.UseSqlServer扩展方法将可用

答案 3 :(得分:9)

它是 1. Microsoft.EntityFrameworkCore(Latest Version) 2. Microsoft.EntityFrameworkCore.SqlServer(1.0.4 Version)

安装以下软件包及其正确版本

{{1}}