2

当我想添加用户时,我使用 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"});
                });
        }
    }
}

这是我的 config.json:

{
    "Data": {
        "DefaultConnection": {
            "Connectionstring": "Data Source=.\\sqlexpress;Initial Catalog=CraftCenter;Integrated Security=True;"
        }
    },
    "EntityFramework": {
        "ApplicationDbContext": {
            "ConnectionStringKey": "Data:DefaultConnection:ConnectionString"
        }
    }

}
4

5 回答 5

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

改变ConnectionStringKeyConnectionString对我有用。

于 2015-03-02T06:09:53.743 回答
3

我更喜欢在 Setup 类中配置存储:

public virtual void ConfigureServices(IServiceCollection services)
{
    services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<ApplicationContext>(options => options.UseSqlServer(
                Configuration.Get("Data:DefaultConnection:ConnectionString")));
     services.AddDefaultIdentity<ApplicationContext, ApplicationUser, IdentityRole>(Configuration);
     services.AddMvc();
}
于 2015-01-05T13:41:34.850 回答
3

我认为问题在于您的 DbContext 类名称与配置中指定的名称不同。

例如,如果您调用了 DbContext,MembershipDbContext您可以通过内部的属性指定它应该使用的连接字符串EntityFramework

{
"Data": {
    "Membership": {
        "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=Membership;Trusted_Connection=True;MultipleActiveResultSets=true"
    }
},
"EntityFramework": {
    "MembershipDbContext": {
        "ConnectionStringKey": "Data:Membership:ConnectionString"
    }
}

这比在代码中指定连接字符串键更干净。

在您的情况下, DbContext 名称ApplicationContext与您在配置中指定的名称ApplicationDbContext不匹配。

于 2015-02-20T21:09:04.683 回答
1

我得到它并在我的上下文中添加此代码:

  protected override void OnConfiguring(DbContextOptions options)
        {
            options.UseSqlServer(Startup.Configuration.Get("Data:DefaultConnection:ConnectionString"));
        }
于 2014-12-28T15:53:13.703 回答
1

它在 DbContext 中寻找连接字符串,因此您需要在 config.json 中将“ConnectionStringKey”更改为“ConnectionString”。

于 2015-02-24T16:09:43.693 回答