我试图在我的appsettings.json文件中编写我的连接字符串并将其带入我的启动文件但我不断得到一个值不能为空.参数名称:connectionString.我一直在使用各种示例,但似乎无法看到ASP.NET 1.0 Core启动类的这个新设置.
Appsetting.json文件:
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Data Source=server;Initial Catalog=dbase;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
尝试Startup.cs的方法
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
var connStr = Configuration.GetConnectionString("DefaultConnection");
System.Console.WriteLine(connStr);
services.AddDbContext<DbContext>(options => options.UseSqlServer(connStr)); //error right here -- Null value
}
Run Code Online (Sandbox Code Playgroud) 目前在Startup中,我的sql server字符串如下所示:
public void ConfigureServices(IServiceCollection services)
{
var connection = @"Server=servername;Database=database;Trusted_Connection=True;MultipleActiveResultSets=true";
services.AddDbContext<CRAMSContext>(options => options.UseSqlServer(connection));
}
Run Code Online (Sandbox Code Playgroud)
我如何使用appsettings.json中的内容:
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Data Source=server;Initial Catalog=database;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
Run Code Online (Sandbox Code Playgroud)
在新的ASP.NET 1.0 CORE新设置中看起来像这样看起来像这样参数化:
public void ConfigureServices(IServiceCollection services)
{
var connection2 = new SqlConnection connectionString;
services.AddDbContext<CRAMSContext>(options => options.UseSqlServer(connection2));
}
Run Code Online (Sandbox Code Playgroud)
另外,如果我有一个不同的测试数据库和qa,我如何知道ASP.NET应用程序为每个环境使用连接?
我的启动类已经在根目录中定义如下:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables(); …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建dbcontext一个数据库来查询数据,但表名称是Table。Table在 SQL Server 中。我没有创建数据库,也无权使用它执行任何操作。
dbcontext如果表中有句点,如何创建一个或一个类?
public DbSet<ADM.INCIDENTS> ADM.INCIDENTS { get; set; }
Run Code Online (Sandbox Code Playgroud)
另外,如果您使用实体框架创建表的类,是否需要命名该类ADM.INCIDENTS.cs?如果你这样做,你会怎么做?
public class ADM.INCIDENTS
{
[Key]
public int ID{ get; set; }
public string Incident{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)