gpt4 book ai didi

asp.net-core - 使用 EF7 命令时 IHostingEnvironment.WebRootPath 为 null

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

问题

当我尝试向代码中添加迁移时,例如:dnx ef migrations add initial,

Startup(IHostingEnvironment env) 中的 env.WebRootPath 为空。

在添加新迁移或更新数据库时,这会给我带来编译错误。

代码

Startup.cs 类中,我在构造函数中有以下几行:

public Startup(IHostingEnvironment env)
{
// ...
MyStaticClass.Initialize(env.WebRootPath);
// ...
_hostingEnvironment = env;
}

此处 env.WebRootPath 为 null,在 Initialize 函数中这会引发异常。

Startup.csConfigureServices 函数中,我解析了我的类依赖项:

public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddInstance<IMyService>(new MyService(_hostingEnvironment.WebRootPath))
// Note: _hostingEnvironment is set in the Startup constructor.
// ...
}

注释

  • 我可以很好地构建、运行和部署代码。一切正常!

  • 但是,我在模型中进行了更改,并希望使用以下命令添加迁移:dnx ef migrations add MyMigration 然后我在包管理器控制台中看到编译错误。

  • 我正在使用 ASP 5 Web 应用程序和 Entity Framework 7

最佳答案

如果 wwwroot 文件夹被无意中从项目的根目录中删除,则 env.WebRootPath 也可能为 null。将 wwwroot 文件夹添加到项目的根目录也可以解决此问题。

关于asp.net-core - 使用 EF7 命令时 IHostingEnvironment.WebRootPath 为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35322136/

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