【发布时间】:2015-11-22 21:13:31
【问题描述】:
我是 EF7 的新手。我知道这是一个重复的问题:
No database providers are configured EF7
但是,在你想结束这个问题之前等一下……然后继续阅读
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddScoped<TestRepository, TestRepository>();
现在我在我的 EF 项目的 cmd 窗口上运行 dnx ef 数据库更新命令并得到这个错误:
C:\TGB.DataAccess>dnx ef database update
System.InvalidOperationException: No database providers are configured. Configure a database provider by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services.
bei Microsoft.Data.Entity.Internal.DatabaseProviderSelector.SelectServices(ServiceProviderSource providerSource)
bei Microsoft.Data.Entity.Internal.DbContextServices.<>c__DisplayClass6_0.<Initialize>b__0()
bei Microsoft.Data.Entity.Internal.LazyRef`1.get_Value()
bei Microsoft.Data.Entity.Internal.DbContextServices.get_DatabaseProviderServices()
bei Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c.<AddEntityFramework>b__0_8(IServiceProvider p)
bei Microsoft.Extensions.DependencyInjection.ServiceLookup.FactoryService.Invoke(ServiceProvider provider)
bei Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
bei Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass12_0.<RealizeService>b__0(ServiceProvider provider)
bei Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
bei Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
bei Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider)
bei Microsoft.Data.Entity.Design.Internal.DesignTimeServicesBuilder.Build(DbContext context)
bei Microsoft.Data.Entity.Design.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
bei Microsoft.Data.Entity.Commands.Program.Executor.<>c__DisplayClass7_0.<UpdateDatabase>b__0()
bei Microsoft.Data.Entity.Commands.Program.Executor.Execute(Action action)
No database providers are configured. Configure a database provider by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services.
现在我尝试根据我粘贴在顶部的解决方案链接更改我的ApplicationDbContext 的构造函数:
这是我的代码:
我的 ApplicationDbContext.cs 实际上是空的,这意味着我没有覆盖任何内容。
查看基类的基类,有带参数 DbContextOptions 的重载构造函数,但我的构造函数不能传递任何东西?!
//
// Summary:
// Initializes a new instance of Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext.
//
// Parameters:
// options:
// The options to be used by a Microsoft.Data.Entity.DbContext.
public IdentityDbContext(DbContextOptions options);
我这边出了什么问题?我正在使用 EF 7 RC1 和 dnx 451。
仅当您将 ApplicationDbContext/ApplicationUser 和整个 Migrations 文件夹移动到一个额外的“DataAccess”项目时才会发生这种情况。然后一切似乎都坏了。
【问题讨论】:
-
您是否尝试指定上下文,例如
dnx database update -c ApplicationDbContext? -
您也可以指定项目
-p <project> -
我通过 CMD 直接进入 DataAccess 项目并运行 dnx ef database update -c ApplicationDbContext => 与以前相同的错误!
-
我对 -p TGB.DataAccess 做了同样的事情 => 和以前一样的错误!
-
我更新了我的问题并删除了我的 ApplicationDbContext 代码,因为我没有使用重载的构造函数,因为没有什么可以重载......我想这就是问题所在。
标签: c# asp.net-core entity-framework-core