EF Core 3.0 手动设置连接字符串

我正在开发一个带有.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;

}


四季花海
浏览 313回答 2
2回答

素胚勾勒不出你

使用正确的工具完成工作 - SqlConnectionStringBuilder 类生成器将转义反斜杠并为 Sql Server 连接构建正确的连接字符串。var builder = new SqlConnectionStringBuilder{&nbsp; &nbsp; DataSource = @"IP_ADDRESS\INSTANCE",&nbsp; &nbsp; InitialCatalog = "DbName",&nbsp; &nbsp; UserID = "MyUserId",&nbsp; &nbsp; Password = "MyPassword"};var connectionString = builder.ConnectionString;// Use connection stringoptionsBuilder.UseSqlServer(connectionString );因此,您的方法可以如下所示:private SqlConnectionStringBuilder BuilderFromElement(XElement source){&nbsp; &nbsp; return new SqlConnectionStringBuilder&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; DataSource = source.Attribute("Server")?.Value,&nbsp; &nbsp; &nbsp; &nbsp; InitialCatalog = source.Attribute("Database")?.Value,&nbsp; &nbsp; &nbsp; &nbsp; UserID = source.Attribute("UserID")?.Value,&nbsp; &nbsp; &nbsp; &nbsp; Password = source.Attribute("Password")?.Value&nbsp; &nbsp; };}private string GetConnectionString(){&nbsp; &nbsp; var settings = XDocument.Load("Settings.xml");&nbsp; &nbsp; var allConnectionStrings =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; settings.Descendants("DataSource")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(BuilderFromElement)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(builder => builder.ConnectionString)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return allConnectionStrings.FirstOrDefault();}

慕森王

查看 xml 文件中反斜杠的用法。在某些上下文中,可以是转义的 \,作为文字,它是 2 个斜杠。\\此代码说明了这一点:这些字符串不相等。&nbsp; &nbsp; &nbsp; &nbsp; String s= "Server=IP\\INSTANCE_NAME;Database=db;User id=User ;Password=pwd;";&nbsp; &nbsp; String s2= @"Server=IP\\INSTANCE_NAME;Database=db;User id=User ;Password=pwd;";&nbsp; &nbsp; Console.WriteLine(s2== s);如果您尝试使用localhost进行此操作,它是否按照您的期望方式工作?如果是这样:罪魁祸首是 .您还可以查看这是否有效。optionsBuilder.UseSqlServer(@"Server=Server=IP\\INSTANCE_NAME;Database=DB;User id=User ;Password=pwd;");希望这有帮助!
打开App,查看更多内容
随时随地看视频慕课网APP