[英]ASP.NET Core 2.2 error when setting up DBContext (works in Kestrel, not in IIS Express)
I have a .Net Core 2.2 web project where I set up the dbcontext connections in the Startup.cs file: 我有一个.Net Core 2.2 Web项目,其中在Startup.cs文件中设置了dbcontext连接:
services.AddDbContext<FirmDirectoryContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultDatabase")));
When I run this is Kestrel it works, but if I try to run it using IIS Express I get the following: 当我运行Kestrel时,它可以工作,但是如果尝试使用IIS Express运行它,则会得到以下信息:
{System.ArgumentNullException: Value cannot be null.
{System.ArgumentNullException:值不能为null。 Parameter name: connectionString
参数名称:connectionString
The connection string is obviously set in the appsettings.json file, or Kestrel would fail as well? 连接字符串显然是在appsettings.json文件中设置的,否则Kestrel也将失败?
I'm not sure how your appsettings.json looks like, but you should have a section in your json file like this : 我不确定您的appsettings.json看起来如何,但是您应该在json文件中像这样设置一个部分:
"ConnectionStrings": {
"DefaultDatabase": "<Your connection string>"
}
If your environment is set as development, dotnet gets connection string from appsettings.development.json. 如果您的环境设置为开发,则dotnet从appsettings.development.json获取连接字符串。
Make sure you have set connection string there as well. 确保在那里也设置了连接字符串。
Looks like the problem was in my Program.cs file. 看来问题出在我的Program.cs文件中。 I had copied some code I used from a .NET Core 2.1 project
我已经复制了.NET Core 2.1项目中使用的一些代码
WebHost.CreateDefaultBuilder(args)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseWebRoot(Directory.GetCurrentDirectory() + "\\wwwroot") // THIS LINE FIXES STATIC FILES ON PROD
.UseIISIntegration()
.UseStartup<Startup>();
Once I edited it to remove the Directory.GetCurrentDirectory() code it works. 一旦对其进行编辑以删除Directory.GetCurrentDirectory()代码,它就会起作用。
WebHost.CreateDefaultBuilder(args)
.UseIISIntegration()
.UseStartup<Startup>();
Now I'll have to test on the server to see if it still works. 现在,我必须在服务器上进行测试以查看其是否仍然有效。 Thanks for the responses.
感谢您的答复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.