我做了很多google,但没有找到任何单一解决方案甚至不在微软的文档中。
这是我的问题。
string connectionString = Configuration.GetConnectionString(DefaultConnection); services.AddDbContext< MyDBContext>(option => option.UseSqlServer(connectionString,m => m.MigrationsAssembly(MyMVCDLL))); services.Configure< AppSettings>(Configuration.GetSection(AppSettings));
<
解决方案首先,不要保存敏感数据(登录,密码,API密钥)在 appsettings.json 中,因为您可能会不小心将其提交给Verison Control,因此可能导致凭据泄漏。为此,您必须使用用户密码工具进行开发,请参阅用户密码文档的详细信息。
其次,请阅读 Configuration.GetConnectionString(DefaultConnection); 方法的工具提示文档。它清楚地表明,GetConnectionString是一个
GetSection(ConnectionStrings)[name]
就是说,你的appsettings.json必须如下所示:
{ ...,ConnectionStrings: {DefaultConnection:Server = myServerAddress; Database = myDataBase; Trusted_Connection = True; } }或使用用户秘密时:
dotnet user-secrets set ConnectionStrings:DefaultConnection Server = myServerAddress; Database = myDataBase; Trusted_Connection = True;
更新
在控制台应用程序中使用它是完全相同的。配置包不是ASP.NET Core特有的,可以自己使用。
所需的软件包(取决于您要使用的软件包
Microsoft.Extensions.Configuration:1.0.0,Microsoft.Extensions.Configuration.EnvironmentVariables:1.0.0,Microsoft.Extensions.Configuration.FileExtensions: 1.0.0,Microsoft.Extensions.Configuration.Json:1.0.0,Microsoft.Extensions.Configuration.UserSecrets:1.0.0,
构建配置的代码与ASP.NET Core完全相同,而不是在启动中执行。 cs 您可以在主要方法中执行此操作:
var builder = new ConfigurationBuilder() .AddJsonFile(appsettings.json,可选:true,reloadOnChange:true) //您不能使用环境特定的配置文件,如 // becuase IHostingEnvironment是一个ASP.NET Core特定的界面 //.AddJsonFile($\"a pp.ettings。{env.EnvironmentName} .json,可选:true) .AddUserSecrets() .AddEnvironmentVariables();
I'm trying to read the appsetting on my console application as well as to set he EntityFramework connection string.
I did a lot of google but did not found any single solution not even on Microsoft's documentation.
Here is the my questions.
string connectionString = Configuration.GetConnectionString("DefaultConnection"); services.AddDbContext<MyDBContext>(option =>option.UseSqlServer(connectionString, m => m.MigrationsAssembly("MyMVCDLL"))); services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
Can someone help me with this.
解决方案First, don't save sensitive data (login, password, API keys) in appsettings.json, as you can accidentally commit it to Verison Control and hence risk your credentials leaking. For this you have to use User Secrets tool for development, see the User Secret documentation for details.
Second, read the tooltip documentation of Configuration.GetConnectionString("DefaultConnection"); method. It clearly states that `GetConnectionString is a
Shorthand for GetSection("ConnectionStrings")[name]
That being said, your appsettings.json has to look like this:
{ ..., "ConnectionStrings": { "DefaultConnection" : "Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;" } }or when using user secrets:
dotnet user-secrets set ConnectionStrings:DefaultConnection Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
Update
Using it in the console application is exactly the same. The configuration package isn't ASP.NET Core specific and can be used on its own.
The required packages are (depending on which of them you want to use
"Microsoft.Extensions.Configuration": "1.0.0", "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0", "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0", "Microsoft.Extensions.Configuration.Json": "1.0.0", "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0",
And the code to build the configuration is exactly the same as in ASP.NET Core. Just instead of doing it in Startup.cs you do it in the Main method:
var builder = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) // You can't use environment specific configuration files like this // becuase IHostingEnvironment is an ASP.NET Core specific interface //.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddUserSecrets() .AddEnvironmentVariables();
更多推荐
在asp网络核心控制台应用程序中阅读appsettings
发布评论