已配置关系存储,而不指定要使用的DbConnection或连接字符串 [英] A relational store has been configured without specifying either the DbConnection or connection string to use
问题描述
我使用asp.net vnext和ef7,当我想添加用户我得到这个错误:
关系存储已经配置没有指定要使用的
DbConnection或连接字符串。
这是什么?
$ X-45454545451-45 X- 20045 X-454545 X-454545 X-454545 X-454545 X-454545 X-454545 X- 20045 X-454545 X-454545 X-454545 X-454545 X-
使用Microsoft.AspNet.Builder;
使用Microsoft.AspNet.Hosting;
使用Microsoft.AspNet.Routing;
使用Microsoft.Framework.ConfigurationModel;
使用Microsoft.Framework.DependencyInjection;
使用Presentation.DataAccessLayer.Context;
使用Microsoft.AspNet.Identity;
命名空间演示
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
Configuration = new Configuration()
.AddJsonFile(config.json)
.AddEnvironmentVariables();
}
public IConfiguration Configuration {get;组;
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFramework()。AddSqlServer()。AddDbContext< ApplicationContext>();
services.AddDefaultIdentity< ApplicationContext,ApplicationUser,IdentityRole>(配置);
services.AddMvc();新新p新新新新新旗新新旗新新旗200新新新旗新新旗200新新新新旗新新名:20045 200新新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗名:
app.UseIdentity();
app.UseMvc(
routes =>
{
routes.MapRoute(default,{controller} / {action} / {id?},
new {controller =Home,action =Index});
});新新p新新新新旗新新旗新新旗新新旗新新旗旗哨式新新旗新新旗新新旗新新旗新新旗旗新新旗新新旗新新旗新新旗新新旗新新旗旗哨旗新新旗新新款: json:
{
Data:{
DefaultConnection:{
Connectionstring:Data Source = .\\sqlexpress;初始目录= CraftCenter; Integrated Security = True;
}
},
EntityFramework:{
ApplicationDbContext:{
ConnectionStringKey:Data:DefaultConnection:ConnectionString
}
}
}
p>我认为问题是你的DbContext类名与你的配置中指定的名称不一样。
例如,如果你有一个叫做$ $ c的DbContext $ c> MembershipDbContext 您可以通过 EntityFramework
中的属性指定应使用的连接字符串。
$ $ $ $ $ $ $$$$$$$$$$$ mssqllocaldb; Database = Membership; Trusted_Connection = True; MultipleActiveResultSets = true
}
},
EntityFramework:{
MembershipDbContext:{
ConnectionStringKey :Data:Membership:ConnectionString
}
}
这比代码中指定连接字符串键更干净。
在您的情况下,DbContext名称为 ApplicationContext
,您在配置中指定的名称是 ApplicationDbContext
,它们不匹配。
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"
}
}
}
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.
这篇关于已配置关系存储,而不指定要使用的DbConnection或连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!