Bin*_*Han 28 asp.net asp.net-mvc asp.net-core
添加迁移时,包管理器控制台中出现以下错误
值不能为空.参数名称:connectionString
这是我的创业公司:
namespace MyProject
{
public class Startup
{
public IConfiguration Configuration { get; set; }
public Startup(IConfiguration config)
{
Configuration = config;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<AppDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddTransient<IDevRepo, DevRepo>();
services.AddMvc();
services.AddMemoryCache();
services.AddSession();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStatusCodePages();
app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
app.Run(async (context) =>
{
await context.Response.WriteAsync(Configuration["Message"]);
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
程序类:
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, builder) => builder.SetBasePath(context.HostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json")
.Build())
.UseStartup<Startup>()
.Build();
}
Run Code Online (Sandbox Code Playgroud)
appsettings.json:
{
"Message": "Hello World",
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=NotMyFault;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
Run Code Online (Sandbox Code Playgroud)
有趣的是,如果我运行应用程序,它会显示"Hello World",但是当添加迁移时,它无法找到connectionString.有人可以在这里点灯吗?谢谢.
Ima*_*our 24
无法找到connectionstring时发生此问题.
你可能在Startup类中有以下代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<BenchmarkContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("yourConnectionString name from appsettings.json")));
}
Run Code Online (Sandbox Code Playgroud)
这些方法解决了您的问题:
1-而不是Configuration.GetConnectionString("yourConnectionString name from appsettings.json")只是把你的连接字符串.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<BenchmarkContext>(options =>
options.UseSqlServer("Data Source=.;Initial Catalog=Benchmark;Persist Security Info=True;User ID=****;Password=****"));
}
Run Code Online (Sandbox Code Playgroud)
2-如果要使用配置文件,请将这些代码添加到Startup类:
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration;
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<BenchmarkContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("TestConnection")));
}
Run Code Online (Sandbox Code Playgroud)
Appsetting.json文件:
{
"ConnectionStrings": {
"TestConnection": "Data Source=.;Initial Catalog=Benchmark;Persist Security Info=True;User ID=****;Password=****"
}
}
Run Code Online (Sandbox Code Playgroud)
之后,在Package Manager Console中执行'add-migration name'命令
小智 9
我有同样的问题,但我的解决方案要简单得多.我所做的只是改变appsettings.json的顺序:
{
"Message": "Hello World",
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=NotMyFault;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
Run Code Online (Sandbox Code Playgroud)
至:
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=NotMyFault;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Message": "Hello World"
}
Run Code Online (Sandbox Code Playgroud)
我怀疑appsettings.json文件中有一个参数序列/顺序.
我在负载测试服务时遇到了这样的问题(我向所有人推荐它)并且有大约 3/1000 个错误的请求,所以我改变了
services.AddDbContextPool<AppDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
Run Code Online (Sandbox Code Playgroud)
到
string connectionString = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContextPool<AppDbContext>(options =>
options.UseSqlServer(connectionString));
Run Code Online (Sandbox Code Playgroud)
所以它读取连接字符串 1 次并且不会在每个请求上使用 Configuration。现在 100% 的请求都成功了。但它似乎是 .Net Core 中的一个错误
| 归档时间: |
|
| 查看次数: |
37540 次 |
| 最近记录: |