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

asp.net asp.net-core c# entity-framework entity-framework-core

Question

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

}

Accepted Answer

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.


Popular Answer

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

Changing ConnectionStringKey to be ConnectionString works for me.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why