【问题标题】:Update-Database using Simple Injector, without services.AddDbContext<>()使用简单注入器的更新数据库,没有 services.AddDbContext<>()
【发布时间】:2020-11-17 14:33:59
【问题描述】:

.NET Core 3.1、EF Core 5.0 预览版 6。

我使用Simple Injector 5.0注册IMyContextFactory(我没有使用services.AddDbContext&lt;&gt;())。

我的MyContext 将所有构造函数设置为私有,因为我使用没有tenantId 的ContextFactory,有时在一个请求中使用几个不同的tenantId,所以我确保每个人都将使用IMyContextFactory 注入而不是MyContext 并防止创建new MyContext() 在控制器等中。

但是Update-Database怎么办?

  1. 虽然我执行Update-Database 时总是出现错误Login failed for user ''.,但我可以毫无问题地从我的WebAPI 项目运行并连接到数据库(我的ConnectionString 很好)。
  2. 我能够在运行时应用迁移,但我必须添加services.AddDbContext&lt;MyContext&gt;(),将MyContext 的构造函数设为公开,并从Program.cs 中的scope.ServiceProvider.GetRequiredService&lt;MyContext&gt;() 获取此上下文,因为我的IMyContextFactory 不在里面ServiceProvider。 (通过https://docs.microsoft.com/pl-pl/ef/core/managing-schemas/migrations/applying?tabs=vs#apply-migrations-at-runtime

如何仅使用 Simple Injector 来实现?

【问题讨论】:

  • 你试过design-time DbContext factory吗?我不确定Update-Database 是否使用它,但也许可以。
  • 是的,我已经实现了它。但是,是的,Update-Database 使用它。谢谢:)

标签: c# asp.net-core entity-framework-core entity-framework-5 simple-injector


【解决方案1】:

解决方案是将 ConnectionString 添加到您的 IDesignTimeDbContextFactory 实现中:

// Fix for "Unable to create an object of type 'MyContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728" error while creating migration
public class MyContextDesignFactory : IDesignTimeDbContextFactory<MyContext>
{
    private string _appsettingsPath = "./../Path/To/ProjectWithAppsettingsJSON";

    public MyContext CreateDbContext(string[] args)
    {
        // In 90% we don't have ASPNETCORE_ENVIRONMENT here, so set fallback to Development
        var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development";

        var config = new ConfigurationBuilder()
            .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), _appsettingsPath))
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{environment}.json", optional: true)
            .Build();

        Console.WriteLine($"Default ConnectionString comes from appsettings file.{Environment.NewLine}You can override it by using `dotnet ef database update --connection MyConnectionString`");

        // Use ConnectionString from appsettings inside WebAPI to fix `Update-Database` command from Package Manager Console
        // We still can override ConnectionString by `dotnet ef database update --connection MyConnectionString`
        var optionsBuilder = new DbContextOptionsBuilder<MyContext>()
            .UseSqlServer(config["ConnectionString"]);

        return MyContext.MyContextFactory.GetMyContext(optionsBuilder.Options);
    }
}

【讨论】:

    【解决方案2】:

    解决方法是安装Entity Framework Core .NET Command-line Tools 并从中更新数据库。

    注意:在 Visual Studio 中禁用所有私有 NuGet 存储库并在包管理器控制台中安装工具:

    dotnet tool install --global dotnet-ef --version 5.0.0-preview.6.20312.4
    

    现在您可以在 Visual Studio 中启用您的私有 NuGet 源。

    然后,转到您的上下文和配置所在的文件夹,然后:

    dotnet ef database update --connection "Your=Connection;String"
    

    您也可以使用Update-Database 更改连接字符串:

    Update-Database -connection "Your=Connection;String"
    

    但这是解决方法。我仍然要求您在“Visual Studio IDE 内部”提供解决方案,而无需安装额外的 CMD 工具

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-25
      • 1970-01-01
      • 2012-05-20
      相关资源
      最近更新 更多