A relational store has been configured without specifying either the DbConnection or connection string to use

不想你离开。 提交于 2020-01-13 19:44:34

问题


I using asp.net vnext and ef7 when i want adding user i getting this error:

A relational store has been configured without specifying either the DbConnection or connection string to use.

What is this?

Consider my app start:

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

and this is my config.json:

{
    "Data": {
        "DefaultConnection": {
            "Connectionstring": "Data Source=.\\sqlexpress;Initial Catalog=CraftCenter;Integrated Security=True;"
        }
    },
    "EntityFramework": {
        "ApplicationDbContext": {
            "ConnectionStringKey": "Data:DefaultConnection:ConnectionString"
        }
    }

}

回答1:


I think the problem was that your DbContext class name is not the same as the name specified in your config.

For example if you have a DbContext called MembershipDbContext you can specify the connection string it should use via the properties inside EntityFramework.

{
"Data": {
    "Membership": {
        "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=Membership;Trusted_Connection=True;MultipleActiveResultSets=true"
    }
},
"EntityFramework": {
    "MembershipDbContext": {
        "ConnectionStringKey": "Data:Membership:ConnectionString"
    }
}

This is cleaner than specifying the connection string keys within the code.

In your case, the DbContext name is ApplicationContext and the name you've specified in the config is ApplicationDbContext which do not match.




回答2:


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

Changing ConnectionStringKey to be ConnectionString works for me.




回答3:


I prefer to configure the store in the Setup class:

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



回答4:


I get it and add this code in my context:

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



回答5:


It's looking for the connection string in DbContext, so you'll need to change "ConnectionStringKey" to "ConnectionString" in the config.json.



来源:https://stackoverflow.com/questions/27677834/a-relational-store-has-been-configured-without-specifying-either-the-dbconnectio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!