我正在开发一个带有.NET Core和EF Core的控制台应用程序(两者都是v3.0);我需要使用从另一个类生成的字符串启动我的DbContext。
DbContext 文件
public Arta_LuniaDBContext() { }
public Arta_LuniaDBContext(DbContextOptions<Arta_LuniaDBContext> options) : base(options) { }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(DataServices.ConnectionString);
}
数据服务类
public static string ConnectionString { get { return GetConnectionData(); } }
private static string GetConnectionData()
{
/// Sets the Server name, Database, UserId and Password.
string Server, Database, UserId, Password;
/// Sets the separator.
string Separator = ";";
/// Connection string [internal].
string ArtaConn = null;
/// Loads Settings.xml
XmlDocument xDoc = new XmlDocument();
xDoc.Load("Settings.xml");
/// Gets the XmlNode: DataSource
XmlNodeList xSource = xDoc.GetElementsByTagName("DataSource");
for (int i = 0; i < xSource.Count; i++)
{
/// Sets the Server name.
Server = "Server=" + xSource[i].Attributes["Server"].Value + Separator;
/// Sets the Database.
Database = "Database=" + xSource[i].Attributes["Database"].Value + Separator;
/// Sets the User id.
UserId = "User id=" + xSource[i].Attributes["UserId"].Value + Separator;
/// Sets the Password.
Password = "Password=" + xSource[i].Attributes["Password"].Value + Separator;
/// Builds the connection string.
ArtaConn = Server + Database + UserId + Password;
Colorful.Console.WriteLine(ArtaConn, System.Drawing.Color.Yellow);
// I'm using this line to test the output.
}
/// Returns~
return ArtaConn;
}
素胚勾勒不出你
慕森王
相关分类