You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.4 KiB
C#
44 lines
1.4 KiB
C#
using DSWeb.Common.Helper;
|
|
using log4net;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Data.Entity;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DSWeb.Common.DB
|
|
{
|
|
public class BaseDbContext : DbContext
|
|
{
|
|
private ILog log = LogManager.GetLogger("BaseDbContext");
|
|
|
|
public BaseDbContext(string conn) : base(conn)
|
|
{
|
|
var getUrl = ConfigurationManager.AppSettings["NacosUrl"];
|
|
var tenant = ConfigurationManager.AppSettings["NacosTenant"];
|
|
var group = ConfigurationManager.AppSettings["NacosGroup"];
|
|
if (!string.IsNullOrEmpty(getUrl))
|
|
{
|
|
if (getUrl.EndsWith("/"))
|
|
{
|
|
getUrl = getUrl.Substring(0, getUrl.Length - 1);
|
|
}
|
|
|
|
var url = getUrl + $"/nacos/v1/cs/configs?dataId={conn}&group={group}&tenant={tenant}";
|
|
try
|
|
{
|
|
var connStr = WebRequestHelper.DoGet(url, timeout: 2000);
|
|
log.Debug($"从{url}获取数据库连接串:{connStr}");
|
|
this.Database.Connection.ConnectionString = connStr;
|
|
}
|
|
catch
|
|
{
|
|
log.Error($"从{url}数据库连接串获取失败,将使用本地配置");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|