【问题标题】:UseSqlServer method missing MVC 6UseSqlServer 方法缺少 MVC 6
【发布时间】:2015-10-11 10:57:19
【问题描述】:

我正在尝试在 MVC 6 中实现实体框架 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>();

        }
    }
}

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    【解决方案2】:

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

    using Microsoft.EntityFrameworkCore;
    

    【讨论】:

    • 谢谢,这正是我所需要的
    • 完美运行...现在遇到另一个问题,说“没有配置数据库提供程序。在设置服务时,通过在 DbContext 类或 AddDbContext 方法中覆盖 OnConfiguring 来配置数据库提供程序。”不知道为什么,因为我的 startup.cs 文件说明了连接字符串?
    • 听起来很容易修复,但这是另一个问题 :)
    • 不是问题 - 需要 @Win 来自另一个答案的评论。
    【解决方案3】:

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

    using Microsoft.EntityFrameworkCore;
    

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

    【讨论】:

      【解决方案4】:

      这是NuGet Packages Problem

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

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

      【讨论】:

        猜你喜欢
        • 2015-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-30
        • 1970-01-01
        • 2015-09-22
        • 2016-04-02
        • 1970-01-01
        相关资源
        最近更新 更多