两个连接 ASP.NET 核心,EF 核心,ArgumentNullException 错误
Two connections ASP.NET Core, EF core, ArgumentNullException error
我的解决方案中有两个项目,其中一个是身份注册,另一个是 ASP.NET Core MVC 项目。我在 appsettings.json 中相应地为 official documentation for connection strings 添加了另一个身份连接。但是我在 ConfigureServices 方法的 Microsoft.EntityFrameworkCore.SqlServer.dll 中发生了异常 System.ArgumentNullException'。
我改了appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\mssqllocaldb;Database=employeedb;Trusted_Connection=True;MultipleActiveResultSets=true",
"LocalConnection": "Server=(localdb)\mssqllocaldb;Database=employeedb;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
}
}
配置服务方法
public void ConfigureServices(IServiceCollection services)
{
services
.AddEntityFrameworkSqlServer()
.AddDbContext<EmployeeContext>(options => {
options.UseSqlServer(Configuration["Data:DefaultConnection"]);
})
.AddDbContext<ApplicationContext>(options => {
options.UseSqlServer(Configuration["Data:LocalConnection"]);
});
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new MappingProfile());
});
IMapper mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<ApplicationContext>();
services.AddScoped<IEmployeeService, EmployeeService>();
}
ConfigureServices 方法异常
如何让两个连接字符串在 ConfigureServices 方法中工作?
您的 appsettings.json 文件中没有 Data:DefaultConnection
元素。相反,请尝试使用 Configuration["ConnectionStrings:DefaultConnection"]
。这对 LocalConnection
也很重要。
使用 GetConnectionString()
扩展方法可能更容易。例如:
Configuration.GetConnectionString("DefaultConnection")
我的解决方案中有两个项目,其中一个是身份注册,另一个是 ASP.NET Core MVC 项目。我在 appsettings.json 中相应地为 official documentation for connection strings 添加了另一个身份连接。但是我在 ConfigureServices 方法的 Microsoft.EntityFrameworkCore.SqlServer.dll 中发生了异常 System.ArgumentNullException'。
我改了appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\mssqllocaldb;Database=employeedb;Trusted_Connection=True;MultipleActiveResultSets=true",
"LocalConnection": "Server=(localdb)\mssqllocaldb;Database=employeedb;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
}
}
配置服务方法
public void ConfigureServices(IServiceCollection services)
{
services
.AddEntityFrameworkSqlServer()
.AddDbContext<EmployeeContext>(options => {
options.UseSqlServer(Configuration["Data:DefaultConnection"]);
})
.AddDbContext<ApplicationContext>(options => {
options.UseSqlServer(Configuration["Data:LocalConnection"]);
});
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new MappingProfile());
});
IMapper mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<ApplicationContext>();
services.AddScoped<IEmployeeService, EmployeeService>();
}
ConfigureServices 方法异常
如何让两个连接字符串在 ConfigureServices 方法中工作?
您的 appsettings.json 文件中没有 Data:DefaultConnection
元素。相反,请尝试使用 Configuration["ConnectionStrings:DefaultConnection"]
。这对 LocalConnection
也很重要。
使用 GetConnectionString()
扩展方法可能更容易。例如:
Configuration.GetConnectionString("DefaultConnection")