使用EF7命令时IHostingEnvironment.WebRootPath为null

编程入门 行业动态 更新时间:2024-10-18 10:13:54
本文介绍了使用EF7命令时IHostingEnvironment.WebRootPath为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

问题

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

When I try to add a migration to my code, e.g: dnx ef migrations add initial,

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

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

This will give me compilation errors when adding a new migration or updating the database.

代码

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

In the Startup.cs class I have these lines in the constructor:

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

此处env.WebRootPath为空,并且在Initialize函数中将引发异常.

Here env.WebRootPath is null and in the Initialize function this throws an exception.

在 Startup.cs 的 ConfigureServices 函数中,我解决了类依赖项:

In the ConfigureServices function of the Startup.cs I resolve my class dependencies:

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

注释

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

  • I can build, run, and deploy the code fine. Everything works!

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

However I made a change in the model and want to add a migration with this command: dnx ef migrations add MyMigration then I see the compilation errors in the package manager console.

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

I am using ASP 5 web application, and Entity Framework 7

推荐答案

在github上有一个关于我的问题的问题报告:

There is an issue reported on github regarding my problem:

github/aspnet/EntityFramework/issues/4494

我现在在评论中使用了变通方法,看来它工作正常:

I used the workaround in the comments now it seems to be working fine:

if (string.IsNullOrWhiteSpace(_env.WebRootPath)) { env.WebRootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"); }

更多推荐

使用EF7命令时IHostingEnvironment.WebRootPath为null

本文发布于:2023-11-08 07:27:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568727.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令   IHostingEnvironment   null   WebRootPath

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!