gpt4 book ai didi

asp.net core 2.0 - 值不能为空。参数名称: connectionString

转载 作者:行者123 更新时间:2023-12-01 20:26:27 27 4
gpt4 key购买 nike

添加迁移时,程序包管理器控制台中出现以下错误

Value cannot be null. Parameter name: 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"]);
});
}
}
}

程序类别:

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();
}

appsettings.json:

{
"Message": "Hello World",
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=NotMyFault;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}

有趣的是,如果我运行该应用程序,它会显示“Hello World”,但是当添加迁移时,它找不到连接字符串。有人可以在这里照亮一些吗?谢谢。

最佳答案

当找不到连接字符串时会出现此问题。

Startup 类中可能有以下代码:

public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<BenchmarkContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("yourConnectionString name from appsettings.json")));
}

这些方法可以解决您的问题:

1-而不是 Configuration.GetConnectionString("yourConnectionString name from appsettings.json(indevelopment mode: 'appsettings.Development.json')") 只需放置您的连接字符串。

public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<BenchmarkContext>(options =>
options.UseSqlServer("Data Source=.;Initial Catalog=Benchmark;Persist Security Info=True;User ID=****;Password=****"));
}

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")));
}

Appsettings.json 文件(在开发模式下:'appsettings.Development.json'):

{
"ConnectionStrings": {
"TestConnection": "Data Source=.;Initial Catalog=Benchmark;Persist Security Info=True;User ID=****;Password=****"
}
}

之后在程序包管理器控制台中执行“add-migration name”命令

关于asp.net core 2.0 - 值不能为空。参数名称: connectionString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46010003/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com