c# - 尝试在 Entity Framework 核心中添加数据库迁移时,为什么会出现 contentRootPath null 异常?

标签 c# .net orm entity-framework-core

我有一个 .NET Core 类库,它是多个 Web 应用程序的数据访问层。我正在使用 Entity Framework Core 1.1,但在通过 PMC 进行迁移时遇到问题。我的初始迁移成功,但在更新数据库并重新启动 VS 2017 后,尝试迁移时,我在 contentRootPath 参数上遇到了 null 异常。成功迁移后,我可能对项目进行了一些更改,但我不记得它们是什么。这是错误和堆栈跟踪:

Both Entity Framework Core and Entity Framework 6 are installed. The Entity Framework Core tools are running. Use 'EntityFramework\Add-Migration' for Entity Framework 6.
System.ArgumentNullException: Value cannot be null.
Parameter name: contentRootPath
   at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value, String parameterName)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations..ctor(IOperationReporter reporter, Assembly assembly, Assembly startupAssembly, String environment, String projectDir, String contentRootPath, String rootNamespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.<>c__DisplayClass4_0.<.ctor>b__4()
   at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)

我的迁移目录中没有配置文件,并且我在 DbContext 类中找不到任何相关内容。有其他人遇到过这个问题,或者知道是什么原因造成的吗?

SqlDbContext 类:

using Microsoft.EntityFrameworkCore;
using DataLayer.Models.User;

namespace DataLayer.Context 
{
  public class SqlDbContext : DbContext
  {
      public DbSet<User> Users { get; set; }

      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
      {
          optionsBuilder.UseSqlServer(MyDbConfig.SqlConnectionString);   
      }

      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Entity<User>()
              .Ignore(m => m.FullName);

          base.OnModelCreating(modelBuilder);
      }
   }
}

最佳答案

事实证明,这是由于 Microsoft.EntityFrameworkCore.Tools 包是预发行版本 2.0.0,目前不是稳定版本。降级到 1.1.1 解决了该问题。

关于c# - 尝试在 Entity Framework 核心中添加数据库迁移时,为什么会出现 contentRootPath null 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44690559/

相关文章:

c# - 将托管函数传递给使用 std::function() 的非托管函数

c# - 如何避免 TCP 套接字在断开连接时抛出 SocketException?

c# - 如何以编程方式设置软件限制策略

java - 这个错误是什么意思以及如何修复它?

javascript - Microsoft OAuth 不返回刷新 token

c# - 单击另一个应用程序中的按钮

php - 如何从 .NET 脚本接收 POST 数据

c# - IIS 7.0 与 7.5 站点 Microsoft.Web.Administration.Site BindingCollection

python - 如何使用 cassandra python-driver 实现基于 token 的分页?

java - @OneToMany 表关系中的冗余行数据或实体