4 c# asp.net entity-framework entity-framework-core asp.net-core
当我想添加用户时,我使用 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"});
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的 config.json:
{
"Data": {
"DefaultConnection": {
"Connectionstring": "Data Source=.\\sqlexpress;Initial Catalog=CraftCenter;Integrated Security=True;"
}
},
"EntityFramework": {
"ApplicationDbContext": {
"ConnectionStringKey": "Data:DefaultConnection:ConnectionString"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我认为问题是您的 DbContext 类名与配置中指定的名称不同。
例如,如果您有一个名为的 DbContext,MembershipDbContext您可以通过 内部的属性指定它应使用的连接字符串EntityFramework。
{
"Data": {
"Membership": {
"ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=Membership;Trusted_Connection=True;MultipleActiveResultSets=true"
}
},
"EntityFramework": {
"MembershipDbContext": {
"ConnectionStringKey": "Data:Membership:ConnectionString"
}
}
Run Code Online (Sandbox Code Playgroud)
这比在代码中指定连接字符串键更清晰。
在您的情况下,DbContext 名称ApplicationContext与您在配置中指定的名称ApplicationDbContext不匹配。
| 归档时间: |
|
| 查看次数: |
12826 次 |
| 最近记录: |