Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.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# 如何为应用程序和迁移使用单独的连接字符串?_C#_Asp.net Core 2.0_Ef Core 2.0 - Fatal编程技术网

C# 如何为应用程序和迁移使用单独的连接字符串?

C# 如何为应用程序和迁移使用单独的连接字符串?,c#,asp.net-core-2.0,ef-core-2.0,C#,Asp.net Core 2.0,Ef Core 2.0,我正在尝试启动并运行带有EntityFramework Core 2.0应用程序的ASP.NET Core 2。作为其中的一部分,我们还希望开始使用迁移来管理数据模型更改 这是我存储连接字符串的appsettings.json文件。为了保持这里的简单,我让user/pwd保持打开状态。在实际场景中,将使用加密。主要目标是使用两个独立的连接字符串。一个用于应用程序使用,其中用户帐户TestAppServiceAccount将仅用于执行读/写操作(仅限DML操作)。另一个名为DbChangeServ

我正在尝试启动并运行带有EntityFramework Core 2.0应用程序的ASP.NET Core 2。作为其中的一部分,我们还希望开始使用迁移来管理数据模型更改

这是我存储连接字符串的appsettings.json文件。为了保持这里的简单,我让user/pwd保持打开状态。在实际场景中,将使用加密。主要目标是使用两个独立的连接字符串。一个用于应用程序使用,其中用户帐户TestAppServiceAccount将仅用于执行读/写操作(仅限DML操作)。另一个名为DbChangeServiceAccount的应用迁移(DML+DDL操作)

下面是我的Startup.cs的设置方式。根据我的理解,应用程序和迁移似乎都将依赖于startup.cs中传递给AddDbContext方法的同一个连接字符串

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            var userServiceSqlConnection = Configuration["SqlServerAppConnection"];
            services.AddDbContext<UserContext>(optiopns => optiopns.UseSqlServer(userServiceSqlConnection));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();
        }
    }
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddMvc();
var userServiceSqlConnection=Configuration[“SqlServerAppConnection”];
services.AddDbContext(optiopns=>optiopns.UseSqlServer(userServiceSqlConnection));
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}

只是想知道如何传递不同的连接字符串,一个用于应用程序,另一个仅用于应用迁移?

发布此问题后,我意识到,我可以使用多环境设置。 因此,对于迁移,我将使用一个单独的环境来管理CI/CD活动。我觉得这是一个部署问题, 因此,我可以简单地创建appsettings.migrations.json或类似的东西,然后回过头来,只为应用程序和迁移使用一个连接字符串。我的Startup.cs AddDbContext参数将保持不变

我的appsettings.development.json如下所示

{
  "SqlServerAppConnection": "Server=SqlServerInstance;Database=TestDb;User Id=TestAppServiceAccount; Password=xyz$123;"  
}
{
    "SqlServerAppConnection": "Server=SqlServerInstance;Database=TestDb;User Id=DbChangeServiceAccount; Password=xyz$123;"
}
我的appsettings.migrations.json如下所示

{
  "SqlServerAppConnection": "Server=SqlServerInstance;Database=TestDb;User Id=TestAppServiceAccount; Password=xyz$123;"  
}
{
    "SqlServerAppConnection": "Server=SqlServerInstance;Database=TestDb;User Id=DbChangeServiceAccount; Password=xyz$123;"
}

Microsoft提供了更多详细信息。

是的,创建的实现并使其访问不同的连接字符串。非常确定您可以在dotnet ef中重写(或者至少是计划好的)conenction字符串commands@Mardoxx,您提到的也很有趣,肯定可以在一些高级用例中使用。谢谢。这真的是最好的方法吗?部署应用程序时,将发布迁移的凭据(具有更高的权限)。必须有一种更好的方法来保持这些独立于web服务器之外。