已配置關係存儲,但未指定要使用的DbConnection或連接字符串


當我想要添加用戶時,我使用asp.net vnext和ef7我收到此錯誤:

已配置關係存儲,但未指定要使用的DbConnection或連接字符串。

這是什麼?

考慮我的應用開始:

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"});
                });
        }
    }
}

這是我的config.json:

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"});
                });
        }
    }
}

一般承認的答案

我認為問題是您的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 ,它們不匹配。


熱門答案

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

ConnectionStringKey更改為ConnectionString對我有用。





許可下: CC-BY-SA
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因