我目前在.net核心2.0中創建了一個Web Api,我簡直不能讓我的連接字符串工作。.Net Core無法從appsettings.json讀取連接字符串
我已經把我的ConnectionString到appsettings.json和startup.cs
加載Appsettings.json
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionStrings": {
"DatabaseConnection": "Data Source=VMDEVSLN-EOE\\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True"
}
}
}
Startup.cs
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
var connection = Configuration.GetConnectionString("DatabaseConnection");
services.AddDbContext<DatabaseContext>(options => options.UseSqlServer(connection));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
的DbContext
public class DatabaseContext : DbContext
{
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
{
}
public DbSet<CustomerTB> CustomerTB { get; set; }
}
我相信問題存在於我的appsettings.json某處,但我只是找不到它
如果你改變'Configuration.GetConnectionString( 「的DatabaseConnection」)什麼;'到 '配置[ 「的ConnectionStrings:的DatabaseConnection」]' – Brivvirs
小號直到相同的錯誤。沒有解決它:( – Ekos
如果連接字符串是從appSettings? – Brivvirs