[英]What is the right way to add app.config to a .net core wpf application since it doesn't come with it unlike .net framework?
I like setting my connection string in my app.config and this has been very easy for me for a .net framework.我喜欢在我的 app.config 中设置我的连接字符串,这对我来说对于 .net 框架来说非常容易。 Since I migrated to .net core I noticed that it doesn't come with app.config.
自从我迁移到 .net core 后,我注意到它没有附带 app.config。
In .NET Core and ASP.NET Core applications the application settings are stored in a JSON file named appsettings.json.在 .NET Core 和 ASP.NET Core 应用程序中,应用程序设置存储在名为 appsettings.json 的 JSON 文件中。 This is the default configuration for .NET Core projects.
这是 .NET Core 项目的默认配置。 INI and XML files is also supported but JSON is the default.
还支持 INI 和 XML 文件,但 JSON 是默认的。 You may also added to .Net Core WPF project appsettings.json config file, for this you need to install following nuget packegs:
您还可以添加到 .Net Core WPF 项目 appsettings.json 配置文件,为此您需要安装以下 nuget packegs:
Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.FileExtensions
Microsoft.Extensions.Configuration.Json
Microsoft.Extensions.DependencyInjection
after this,在这之后,
{
"ConnectionStrings": {
"myDataBaseConnectionString": "Server=myServer;Database=myDataBase;Trusted_Connection=True;"
}
}
and you can read it in your code by calling the GetConnectionString method in the Microsoft.Extensions.Configuration namespace.您可以通过调用 Microsoft.Extensions.Configuration 命名空间中的 GetConnectionString 方法在代码中读取它。
var myDbConnectionString = _configuration.GetConnectionString("myDataBaseConnectionString");
Second way, you may use static class for keeping your database connection data:第二种方式,您可以使用静态类来保存您的数据库连接数据:
public static class dbConfig
{
public const string ConnectionString =
"Data Source = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (COMMUNITY = TCP)(PROTOCOL = TCP)(HOST =192.100.10.11)(PORT = 1521)))(CONNECT_DATA = (SID = testdb))); User Id =admin; Password=admin;Pooling=true";
}
and you may use from it any place of your assambly你可以在任何地方使用它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.