我在使用 asp.net vnext 和 ef7 时想要添加用户时出现此错误:
A relational store has been configured without specifying either the DbConnection or connection string to use.
这是什么?
考虑我的应用启动:
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:
{
"Data": {
"DefaultConnection": {
"Connectionstring": "Data Source=.\\sqlexpress;Initial Catalog=CraftCenter;Integrated Security=True;"
}
},
"EntityFramework": {
"ApplicationDbContext": {
"ConnectionStringKey": "Data:DefaultConnection:ConnectionString"
}
}
}
最佳答案
我认为问题在于您的 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
,这不匹配。
关于c# - 已配置关系存储,但未指定要使用的 DbConnection 或连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27677834/