C# 已配置关系存储,但未指定要使用的DbConnection或连接字符串

C# 已配置关系存储,但未指定要使用的DbConnection或连接字符串,c#,asp.net,entity-framework,asp.net-core,entity-framework-core,C#,Asp.net,Entity Framework,Asp.net Core,Entity Framework Core,当我想添加用户时,我使用asp.net vnext和ef7,我收到以下错误: 已配置关系存储,但未指定 要使用的数据库连接或连接字符串 这是什么 考虑我的应用程序启动: using DomainModels; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Routing; using Microsoft.Framework.ConfigurationModel; usi

当我想添加用户时,我使用asp.net vnext和ef7,我收到以下错误:

已配置关系存储,但未指定 要使用的数据库连接或连接字符串

这是什么

考虑我的应用程序启动:

using DomainModels;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
using Presentation.DataAccessLayer.Context;
using Microsoft.AspNet.Identity;
namespace Presentation
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            Configuration = new Configuration()
                .AddJsonFile("config.json")
                .AddEnvironmentVariables();
        }

        public IConfiguration Configuration { get; set; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddEntityFramework().AddSqlServer().AddDbContext<ApplicationContext>();
            services.AddDefaultIdentity<ApplicationContext, ApplicationUser, IdentityRole>(Configuration);
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseStaticFiles();
            app.UseIdentity();
            app.UseMvc(
                routes =>
                {
                    routes.MapRoute("default", "{controller}/{action}/{id?}",
                        new {controller = "Home", action = "Index"});
                });
        }
    }
}

我得到它并在我的上下文中添加以下代码:

  protected override void OnConfiguring(DbContextOptions options)
        {
            options.UseSqlServer(Startup.Configuration.Get("Data:DefaultConnection:ConnectionString"));
        }

我更喜欢在Setup类中配置存储:

public virtual void ConfigureServices(IServiceCollection services)
{
    services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<ApplicationContext>(options => options.UseSqlServer(
                Configuration.Get("Data:DefaultConnection:ConnectionString")));
     services.AddDefaultIdentity<ApplicationContext, ApplicationUser, IdentityRole>(Configuration);
     services.AddMvc();
}
公共虚拟void配置服务(IServiceCollection服务)
{
services.AddEntityFramework()
.AddSqlServer()文件
.AddDbContext(选项=>options.UseSqlServer(
Get(“数据:DefaultConnection:ConnectionString”);
services.AddDefaultIdentity(配置);
services.AddMvc();
}

我认为问题在于DbContext类名与配置中指定的名称不同

例如,如果有一个名为
MembershipDbContext
的DbContext,则可以通过
EntityFramework
中的属性指定它应该使用的连接字符串

{
"Data": {
    "Membership": {
        "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=Membership;Trusted_Connection=True;MultipleActiveResultSets=true"
    }
},
"EntityFramework": {
    "MembershipDbContext": {
        "ConnectionStringKey": "Data:Membership:ConnectionString"
    }
}
这比在代码中指定连接字符串键更简洁


在您的例子中,DbContext名称是
ApplicationContext
,而您在配置中指定的名称是
ApplicationDbContext
,两者不匹配。

它正在DbContext中查找连接字符串,因此您需要在config.json中将“ConnectionString”更改为“ConnectionString”

"EntityFramework": {
    "MembershipDbContext": {
        "ConnectionStringKey": "Data:Membership:ConnectionString"
    }
}

ConnectionStringKey
更改为
ConnectionString
对我有效。

这没有意义。如果这是在上下文类中,那么Startup就不是一个静态类。我遇到了与Shawn相同的问题,其中这不是一个静态类。你有没有找到一个干净的解决方案?将DbContext添加到解决方案中的单独项目时出现相同问题。@aredkid下面的答案更可取,将配置保留在web项目中,而不在上下文中。我还将DbContext移动到另一个程序集并重命名了它,但从未注意到config.json文件试图按名称匹配它(可能是为了支持多个上下文场景)。由于它是在config.json文件中定义的,所以该方法不应该没有任何更改吗?与接受的答案相比,我更喜欢使用该方法,因为它将配置保留在Startup类中,但是我收到一条错误消息,即“ASP.NET Core 5.0”中不提供该方法。(应用程序在没有设置的情况下可以正常工作,但scaffolding因为我没有“指定要使用的DbConnection或连接字符串”而对我大发雷霆)。纯粹的天才,我甚至没有注意到名称与上下文匹配,因为我将它(上下文)移动到另一个dll并重命名了它,这是我丢失的链接。非常感谢。
"EntityFramework": {
    "MembershipDbContext": {
        "ConnectionStringKey": "Data:Membership:ConnectionString"
    }
}