18 lines
747 B
Python
18 lines
747 B
Python
import json
|
|
from pathlib import Path
|
|
|
|
|
|
def get_connection_string(name="FirstSolutionDB", settings_path=None):
|
|
"""Load connection string from appsettings.json in the parent project."""
|
|
if settings_path is None:
|
|
local_settings = Path(__file__).resolve().parents[1] / "appsettings.json"
|
|
settings_path = local_settings if local_settings.exists() else Path(__file__).resolve().parents[2] / "appsettings.json"
|
|
else:
|
|
settings_path = Path(settings_path)
|
|
|
|
data = json.loads(settings_path.read_text(encoding="utf-8"))
|
|
conn_strings = data.get("ConnectionStrings", {})
|
|
if name not in conn_strings:
|
|
raise KeyError(f"Connection string '{name}' not found in {settings_path}")
|
|
return conn_strings[name]
|