這里是appsettings.json檔案中的連接字串。
appsettings.json
"ConnectionStrings"/span>: {
"DefaultConnection": "Server=DESKTOP-11G3852SQLEXPRESS; Database=MyDB; Trusted_Connection=True; MultipleActiveResultSets=true"。
"OfficeConnection"。"Server=DESKTOP-DTUS54A;Database=MyDB; Trusted_Connection=True;MultipleActiveResultSets=true"。
"LappyConnection"。"Server=DESKTOP-J8PN84H;Database=MyDB;Trusted_Connection=True;MultipleActiveResultSets=true"。
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection") )。
.....
這里的問題是,每次當我改變我的機器時,我必須在Sratup.cs檔案中手動設定連接。 在這里你可以看到我設定了"DefaultConnection".
。我想要的解決方案是,我在appsetings.json檔案中設定了許多連接字串(現在我有3個連接字串),它將檢查連接是否與第一個連接字串(DefaultConnection)建立。
如果沒有,它將開始用第二個連接字串(OfficeConnection)建立連接。
如果連接建立成功,我的網站應該繼續使用第二個連接字串(OfficeConnection)。
如果連接成功,我的網站應該繼續使用第二個連接字串(OfficeConnection)。
額外: 如果可能的話我的遷移也需要應用成功連接的連接字串。
uj5u.com熱心網友回復:
這不是正確的方法。組態檔的存在正是為了這個原因,那就是標志著它們在哪個環境下運行:查看這里的答案。ASP.NET Core 在構建程序中設定托管環境
因此,正確的解決方案是根據你運行的環境來更改組態檔。
如果你真的想檢查連接性,你可以試試這樣的方法。以編程方式測驗 SQL Server 連接的最佳方法是什么?
// <summary>
// 測驗服務器已連接。
// </summary>/span>
// <param name="connectionString">/span>連接字串</param>
// <returns>如果連接已打開,則為true</returns>
private static bool IsServerConnected(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try (SqlConnection connection = new SqlConnection(connectionString)
{
connection.Open()。
return true;
}
catch (SqlException)
{
return false;
}
}
對于你的每一個連接字串,并作出相應的選擇。
實話實說,你不應該在配置期間做這些事情,因為這將花費一些時間,并使你的應用程式看起來沒有反應。
uj5u.com熱心網友回復:
我通過他的解決方案得到了這個概念,然后我就滿足了我的要求。
這是一個解決方案。
這里是你想要的解決方案,只需將代碼粘貼到你的檔案中
//START
...
//end
下面的解決方案中的block:
using System;
using System.Collections.Generic;
using MySystem.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.Data.SqlClient;
使用System.Configuration。
namespace MySystem
{
public class Startup {
{
public Startup(IConfiguration configuration)
{
配置 = 配置。
//START
ConnectionStrings = Configuration.GetSection("ConnectionStrings").GetChildren().ToDictionary(x => x.Key, x => x.Value) 。
ConnString = ""。
foreach (var item in ConnectionStrings)
{
if (IsServerConnected(item.Value))
{
ConnString = item.Value;
break;
}
}
//END; }
}
public IConfiguration Configuration { get; }
//START。
public string ConnString { get; }
public static Dictionary<string, string> ConnectionStrings { get; private set; }
//end; }
///此方法被運行時呼叫。使用此方法來向容器添加服務。
public void ConfigureServices(IServiceCollection services)
{
//services.AddDbContext<ApplicationDbContext>(options =>/span>
//選項.UseSqlServer()
// Configuration.GetConnectionString("DefaultConnection"));
//START
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
ConnString))。
//END
.......
services.AddRazorPages()。
}
//此方法被運行時呼叫。使用此方法來配置HTTP請求管道。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
......
}
//START
private static bool IsServerConnected(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try (SqlConnection connection = new SqlConnection(connectionString)
{
connection.Open()。
return true;
}
catch (SqlException)
{
return false;
}
finally; }
{
connection.Close()。
}
}
}
//end。
}
}
uj5u.com熱心網友回復:
你可以創建多個appsetting.json檔案,并在午餐設定中指定你將使用的檔案。在根appsettings中放入不會改變的部分。例如appsettings.office.json,你只在這種情況下改變連接。但如果你想自動改變它,我認為正確的路徑是在你的每個環境中使用一個環境變數來設定。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/320354.html
標籤:
