Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用简单的注入器更新数据库,不使用服务。AddDbContext<&燃气轮机;()_C#_Asp.net Core_Entity Framework Core_Entity Framework 5_Simple Injector - Fatal编程技术网

C# 使用简单的注入器更新数据库,不使用服务。AddDbContext<&燃气轮机;()

C# 使用简单的注入器更新数据库,不使用服务。AddDbContext<&燃气轮机;(),c#,asp.net-core,entity-framework-core,entity-framework-5,simple-injector,C#,Asp.net Core,Entity Framework Core,Entity Framework 5,Simple Injector,.NET Core 3.1、EF Core 5.0预览版6 我正在使用注册IMyContextFactory(我没有使用services.AddDbContext()) 我的MyContext将所有构造函数都设置为private,因为我使用的ContextFactory没有tenantId,有时在一个请求中几乎没有不同的tenantId,所以我确保每个人都将使用IMyContextFactory注入,而不是MyContext,并防止创建新的MyContext()在控制器等中 但是如何更新数据库呢

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

我正在使用注册
IMyContextFactory
(我没有使用
services.AddDbContext()

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

但是如何更新数据库呢

  • 当我执行
    更新数据库时
    对于用户“”,我总是出现错误
    登录失败。
    ,但我可以从我的WebAPI项目运行并连接到数据库,没有任何问题(我的连接字符串很好)
  • 我能够在运行时应用迁移,但我必须添加
    服务.AddDbContext()
    ,将
    MyContext
    的构造函数设置为公共,并从Program.cs中的
    scope.ServiceProvider.GetRequiredService()
    获取此上下文,因为我的
    IMyContextFactory
    不在
    ServiceProvider
    内。(via)

  • 如何仅使用简单的注入器来实现这一点?

    解决方法是安装并从中更新数据库

    注意:在Visual Studio中禁用所有私有NuGet存储库,并在Package Manager控制台中安装工具:

    dotnet tool install --global dotnet-ef --version 5.0.0-preview.6.20312.4
    
    现在,您可以在VisualStudio中启用您的私有NuGet提要

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

    dotnet ef database update --connection "Your=Connection;String"
    
    您还可以使用
    更新数据库
    更改连接字符串:

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

    但这是解决办法。我仍然要求您提供Visual Studio IDE“内部”解决方案,而无需安装其他CMD工具。解决方案是将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);
        }
    }
    
    //修复“无法创建'MyContext'类型的对象”。有关设计时支持的不同模式,请参阅https://go.microsoft.com/fwlink/?linkid=851728“创建迁移时出错
    公共类MyContextDesignFactory:IDesignTimeDbContextFactory
    {
    私有字符串_appsetingspath=“../../Path/To/projectwithappsetingsjson”;
    公共MyContext CreateDbContext(字符串[]args)
    {
    //在90%的情况下,我们这里没有ASPNETCORE_环境,所以请回退到开发阶段
    var-environment=environment.GetEnvironmentVariable(“ASPNETCORE_-environment”)??“Development”;
    var config=new ConfigurationBuilder()
    .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(),_appsetingspath))
    .AddJsonFile(“appsettings.json”,可选:false,reloadOnChange:true)
    .AddJsonFile($“appsettings.{environment}.json”,可选:true)
    .Build();
    Console.WriteLine($”默认连接字符串来自appsettings文件。{Environment.NewLine}您可以使用`dotnet ef database update--connection MyConnectionString``覆盖它);
    //在WebAPI中使用appsettings中的ConnectionString来修复Package Manager控制台中的“更新数据库”命令
    //我们仍然可以通过`dotnet ef database update--connection MyConnectionString来覆盖ConnectionString`
    var optionsBuilder=new DbContextOptionsBuilder()
    .UseSqlServer(配置[“连接字符串]);
    返回MyContext.MyContextFactory.GetMyContext(optionsBuilder.Options);
    }
    }
    
    您试过了吗?我不确定更新数据库是否使用它,但也许可以。是的,我已经实现了它。但是是的,
    updatedatabase
    使用它。谢谢:)