c# - Entity Framework 7 未配置数据库提供程序 => 当迁移移动到另一个项目时

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

我是 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”项目时才会发生这种情况。然后一切似乎都坏了。

最佳答案

  1. 使用 Web 应用程序模板(身份验证:个人用户帐户)创建一个名为“WebProject”的新 ASP.NET Web 应用程序。

  2. 现在将另一个项目添加到名为“DataAccess”的解决方案(假设类库类型虽然不相关)

  3. 现在将 ApplicationDbContext/ApplicationUser 文件和迁移文件夹移动到 DataAccess 项目。

  4. 在此阶段构建将失败,因此我们需要更正 project.json 以获得正确的引用。

  5. 对于DataAccess项目,添加如下依赖

    "dependencies": {
        "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final",
        "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final"
    }
    
  6. 对于 WebProject,添加 DataAccess 作为依赖项。由于 DataAccess 引用了以上 2 个包,您可以从 WebProject 中删除这些引用。
    构建现在应该成功,您可以启动 Web 应用程序。

如何使用 ef 迁移:

在命令行中,转到 WebProject 目录(而不是 DataAccess 项目)。 从这里开始,所有 ef 命令都可以正常工作。

要将新的迁移添加到 DataAccess 项目的 Migrations 文件夹中,您需要使用 -p 标志。喜欢,
dnx ef migrations add Sample -p DataAccess

问题的出现是因为命令是从 DataAccess 项目中的 cmd 运行的。初始化所有服务的启动类在 WebProject 中。当您尝试从 DataAccess 目录运行命令时,dnx ef 将被识别,因为 EntityFramework.Commands 被引用但是当您尝试使用迁移时,服务未初始化因此它将无法抛出上述异常。

关于c# - Entity Framework 7 未配置数据库提供程序 => 当迁移移动到另一个项目时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33860263/

相关文章:

c# - DataGrid ScrollIntoView - 如何滚动到未显示的第一行?

c# - 快速视频显示 WPF

asp.net - 路径长度太长时 TeamCity DNU 发布失败

asp.net-core - ASP.NET 5 概念问题

postgresql - 使用 NetTopologySuite 与 EF Core 和 postgis 计算两点之间的地理距离

c# - 如何处理大型代码库中的异常

c# - .NET 自定义控件 (ToolStripControlHost) 对设计人员造成严重破坏

sql-server - Entity Framework Core 5 中多对多关系的 DeleteBehavior

asp.net - 如何将 IConfigurationRoot 或 IConfigurationSection 转换为 JObject/JSON

c# - AspNetCore 2.1 Bearer Token Authentication - 当前用户为空