I'm new to EF and am trying to create a simple test solution using VS 2017, .NET Core 2.0 and EF 2.0 but I can't get add-migration to create the migrations folder and the initial migration.
I have created a solution called Driver with two .Net Core class library projects:
Driver.Data
Driver.Domain
The following packages are installed in Driver.Data:
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
Driver.Data has a single cs file called Driver.Context:
using Driver.Domain;
using Microsoft.EntityFrameworkCore;
namespace Driver.Data
{
public class DriverContext : DbContext
{
public DbSet<Company> Companies { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(
"Server=(localdb)\\mssqllocaldb; Database=Driver; Trusted_Connection=True;"
);
}
}
}
Driver.Domain has a single cs file called Company.cs:
namespace Driver.Domain
{
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string State { get; set; }
}
}
I've set the startup project to Driver.Data and in the PM Console I've set the Default Project to Driver.Data
Here is the output of add-migration init -verbose:
Using project 'Driver.Data'.
Using startup project 'Driver.Data'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile "C:\Users\alex.florin\Documents\Visual Studio 2017\Projects\Driver\trunk\Driver.Data\bin\Debug\netcoreapp2.0\Driver.Data.deps.json" --additionalprobingpath C:\Users\alex.florin\.nuget\packages --additionalprobingpath "C:\Program Files (x86)\Microsoft SDKs\NuGetPackagesFallback" --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --fx-version 2.0 "C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.entityframeworkcore.tools\2.0.1\tools\netcoreapp2.0\ef.dll" migrations add init --json --verbose --no-color --prefix-output --assembly "C:\Users\alex.florin\Documents\Visual Studio 2017\Projects\Driver\trunk\Driver.Data\bin\Debug\netcoreapp2.0\Driver.Data.dll" --startup-assembly "C:\Users\alex.florin\Documents\Visual Studio 2017\Projects\Driver\trunk\Driver.Data\bin\Debug\netcoreapp2.0\Driver.Data.dll" --project-dir "C:\Users\alex.florin\Documents\Visual Studio 2017\Projects\Driver\trunk\Driver.Data\\" --root-namespace Driver.Data
There are no errors but nothing is being generated.
@JulieLerman provided the answer when I asked in her pluralsight course discussion. There is apparently an issue with attempting this with the .NET Core class libraries. The successful workaround is to put the following in the csproj file of the DBContext project:
<PropertyGroup>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
</PropertyGroup>
She wrote a blog about it: http://thedatafarm.com/data-access/the-secret-to-running-ef-core-2-0-migrations-from-a-net-core-or-net-standard-class-library/
Additionally, be sure to set the project with the DBContext in it as the startup project
I had issues when I attempt to get it to work as well as far as I am concerned EF Core 2.0 is not fully ready for prime time as the main dotnet core commands don't work out of the box. In addition to what you did I had to run a few more things to get it to work in the project file itself. I followed these two threads: https://github.com/aspnet/Scaffolding/issues/645 and Joe Healy's answer here: No executable found matching command "dotnet-ef"
In essence it would do nothing or give a very basic error. It ended up being that the hook up of the Microsoft.EntityFrameworkCore.Tools.DotNet were not working correctly and needed to be set in the 'ItemGroup' in my .NET Core 2 project like so:
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.1" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1" />
</ItemGroup>
I also believe you need to use a slightly different code for making the migrations work utilizing dotnet that calls out commands for 'dotnet core'. Followed by 'ef'. So I just did this on my test solution:
dotnet ef migrations add test
and it worked. You must, must, be in the same folder level as your project when doing the commands for migrations or database updates. If you are still having trouble feel free to take a look at a project I created on GitHub:
https://github.com/djangojazz/EFCoreTest/tree/master/EFCoreCodeFirstScaffolding
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With