c# - 已配置关系存储,但未指定要使用的 DbConnection 或连接字符串

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

我在使用 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/

相关文章:

c# - 如何将此 SQL 查询转换为 LINQ (OVER (PARTITION BY Date))

c# - ServiceStack.OrmLite : using aliases in SqlExpression for anonymous types

c# - 无法解析 CreatedAtAction(nameof(ActionName)) 中的操作

C# - 如何使用 SendKeys.Send 通过 SHIFT 键发送小写字母?

c# - Window.Print() 阻止其他 google chrome 标签

c# - 如何在 EF 4.1 RC 中的 DbContext 级别关闭更改跟踪?

asp.net - 管道模式和池标识是否会影响使用集成安全性运行的应用程序?

asp.net - 在 ASP.NET 中自定义 CSS 到 SliderExtender

c# - 处理对数据库的重复 linq 查询的正确做法应该是什么?

database - 为什么我可以使用 Dapper 而不是 Entity Framework 来拥有多个数据读取器?