I have a .NET Core class library that is the data access layer for multiple web applications. I am using Entity Framework Core 1.1, but I'm having trouble with migrations through PMC. My initial migration worked successfully, but after updating the database and re-launching VS 2017 I get a null exception on the contentRootPath argument when attempting to migrate. I may have made some changes to the project after my successful migration, but I don't remember what they were. This is the error and stack trace:
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)
There is no config file in my migrations directory, and I can't find anything related in the DbContext class. Has anyone else ran into this issue, or know what is causing it?
SqlDbContext class:
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);
}
}
}
It turns out that this was due to the Microsoft.EntityFrameworkCore.Tools
package being pre-release version 2.0.0, which is not currently a stable version. Downgrading to 1.1.1 fixed the issue.
I had this issue in the past and here is the fix:
First, make sure that the version of the SDK you are using is 2.1.200 as you are under Core 1.1 and put this on your global.json:
{
"sdk": {
"version": "2.1.200"
}
}
Add reference on your .csproj to Microsoft.EntityFrameworkCore.Design
and Microsoft.EntityFrameworkCore.Tools.DotNet
so you will have:
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design"
Version="1.1.6"
PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet"
Version="1.1.6" />
</ItemGroup>