从 appsettings.json 到 Entity Framework .NET Core 中的 OnConfiguring 方法的连接字符串
Connection string from appsettings.json to OnConfiguring method in Entity Framework .NET Core
如我所见,我无法通过此方法访问 IConfiguration 注入的对象。此外,正如我所见,无法访问 HttpContext 并从那里进入。有什么简单的方法可以获取存储在 appsettings.json 中的连接字符串?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Projekt.Models;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Http;
namespace Project.DAL
{
public class UserContext : DbContext
{
public DbSet<User> Users {get;set;}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string connection_string = ""; //connection string from appsettings.json
optionsBuilder.UseSqlServer(connection_string);
}
public class User {
public string login{ get; set; }
public string password { get; set; }
}
}
}
据我所知,您可以在任何需要的地方注入 IConfiguration:
private readonly IConfiguration _configuration;
public MyClass(IConfiguration configuration)
{
_configuration = configuration;
}
但是在Startup.ConfigureServices中这样做没有错:
string connection = _configuration.GetConnectionString("Main");
services.AddDbContext<UserContext >(options => options.UseSqlServer(connection));
您必须将 IConfiguration
注入 Startup
构造函数:
public class Startup
{
public IConfiguration Configuration { get; }
public IWebHostEnvironment Environment { get; }
public Startup(IConfiguration configuration, IWebHostEnvironment environment)
{
Configuration = configuration;
Environment = environment;
}
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureDatabase(Configuration);
// ...other services
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ...middlewares
}
}
请注意,我使用了一种扩展方法来配置我的 dbContext,这使得 Startup 更干净:
public static class ServiceExtensions
{
public static void ConfigureDatabase(this IServiceCollection services, IConfiguration configuration)
{
services.AddDbContext<MyDbContext>(options =>
{
string connectionString = configuration.GetConnectionString("sqlConnection") + ";password=" + configuration["Passwords:Database"];
options.UseSqlServer(connectionString);
});
}
}
首先,您必须在 appsettings.json
中设置连接字符串
"ConnectionStrings": {
"sqlConnection": "server=\SQLEXPRESS; database=AKKADatabase; Integrated Security=true"
},
而你的startup.cs你必须配置你的连接
public class Startup
{
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.AddDbContext<ApplicationContext>(option => option.UseSqlServer(Configuration.GetConnectionString("sqlConnection")));
services.AddControllersWithViews();
}
}
在您可以在数据库上下文中使用它之后:
public class ApplicationContext : DbContext
{
public ApplicationContext(DbContextOptions options)
: base(options)
{
}
}
如我所见,我无法通过此方法访问 IConfiguration 注入的对象。此外,正如我所见,无法访问 HttpContext 并从那里进入。有什么简单的方法可以获取存储在 appsettings.json 中的连接字符串?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Projekt.Models;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Http;
namespace Project.DAL
{
public class UserContext : DbContext
{
public DbSet<User> Users {get;set;}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string connection_string = ""; //connection string from appsettings.json
optionsBuilder.UseSqlServer(connection_string);
}
public class User {
public string login{ get; set; }
public string password { get; set; }
}
}
}
据我所知,您可以在任何需要的地方注入 IConfiguration:
private readonly IConfiguration _configuration;
public MyClass(IConfiguration configuration)
{
_configuration = configuration;
}
但是在Startup.ConfigureServices中这样做没有错:
string connection = _configuration.GetConnectionString("Main");
services.AddDbContext<UserContext >(options => options.UseSqlServer(connection));
您必须将 IConfiguration
注入 Startup
构造函数:
public class Startup
{
public IConfiguration Configuration { get; }
public IWebHostEnvironment Environment { get; }
public Startup(IConfiguration configuration, IWebHostEnvironment environment)
{
Configuration = configuration;
Environment = environment;
}
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureDatabase(Configuration);
// ...other services
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ...middlewares
}
}
请注意,我使用了一种扩展方法来配置我的 dbContext,这使得 Startup 更干净:
public static class ServiceExtensions
{
public static void ConfigureDatabase(this IServiceCollection services, IConfiguration configuration)
{
services.AddDbContext<MyDbContext>(options =>
{
string connectionString = configuration.GetConnectionString("sqlConnection") + ";password=" + configuration["Passwords:Database"];
options.UseSqlServer(connectionString);
});
}
}
首先,您必须在 appsettings.json
中设置连接字符串"ConnectionStrings": {
"sqlConnection": "server=\SQLEXPRESS; database=AKKADatabase; Integrated Security=true"
},
而你的startup.cs你必须配置你的连接
public class Startup
{
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.AddDbContext<ApplicationContext>(option => option.UseSqlServer(Configuration.GetConnectionString("sqlConnection")));
services.AddControllersWithViews();
}
} 在您可以在数据库上下文中使用它之后:
public class ApplicationContext : DbContext
{
public ApplicationContext(DbContextOptions options)
: base(options)
{
}
}