10
votes

I'm getting an error when trying to Add-Migration for Entity Framework Core, to a Code First project, here are the details...

I have created a new ASP.Net Core web project (Core 2.0 in VS 2017). It's using Microsoft.AspNetCore.All dependency, shown below:

enter image description here

I am looking to utilize the Entity Framework Core (my understanding was that the All metadata had the EF Core dependencies included already, shown below, looks to be correct):

enter image description here

I've setup my entities and context and I've ensured the DB is setup using the following code.

Example Model

public class City
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required]
    [MaxLength(50)]
    public string Name { get; set; }

    [MaxLength(200)]
    public string Description { get; set; }
}

Example Context

public class CityInfoContext : DbContext
{
    public DbSet<City> Cities { get; set; }
    public DbSet<PointOfInterest> PointsOfInterest { get; set; }

    public CityInfoContext(DbContextOptions options) : base(options)
    {
        Database.EnsureCreated();
    }
}

Startup.cs Config

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()
    .AddMvcOptions(options => {
        options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
    })
    .AddJsonOptions(options => {
        if (options.SerializerSettings.ContractResolver != null)
        {
            var res = options.SerializerSettings.ContractResolver as DefaultContractResolver;
            res.NamingStrategy = null;
        }
    });

    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    services.AddSingleton<IMailService, LocalMailService>();

    // Entity Framework services.
    var connectionString = @"Server=(localdb)\mssqllocaldb;Database=CityInfoDB;Trusted_Connection=True;";
    services.AddDbContext<CityInfoContext>(options => options.UseSqlServer(connectionString));
}

Initializing the dB context with this line in my controller:

public class DummyController: Controller
{
    CityInfoContext _ctx;

    public DummyController(CityInfoContext ctx)
    {
        _ctx = ctx;
    }
}

I can see the db is created successfully - all good so far.

enter image description here

I want to take a snapshot of my db using this command: PM> Add-Migration CityInfoInitialMigration

But get the error: The EntityFramework package is not installed on project 'CityInfo.API'.

enter image description here

Has anyone come across this before? I explicitly tried adding the EF packages but that didn't work either!

4
It looks like the EF6 PMC commands are running... What does Get-Module say is loaded? - bricelam

4 Answers

17
votes
NPM> Get-Module

If the result contains EntityFramework

ModuleType Version    Name                                ExportedCommands                                                                                                                                                                                         
---------- -------    ----                                ----------------                                                                                                                                                                                         
Script     6.0.0.0    EntityFramework                     {Add-    EFDefaultConnectionFactory, Add-EFProvider, Add-Migration, Enable-Migrations...}                                                                                                                    
Script     2.0.0      EntityFrameworkCore                 {Add-Migration, Drop-Database, Enable-Migrations, Get-DbContext...}                                                                                                                                      
Script     2.0.0.0    NuGet                               {Add-BindingRedirect, Find-Package, Get-Package, Get-Project...}                                                                                                                                         
Script     0.0        profile                                                                                                                                                                                                                                      

Meaning both EntityFrameworkCore and EntityFramework Nuget packages are installed in a project and that causes

The EntityFramework package is not installed

In my case, I was referencing some Nuget package which was referencing EntityFramework 6.0.0 (so EntityFramework package was referenced indirectly). After removing that package, the error was fixed.

The easiest way to find such reference is using Search Solution Explorer dialog

enter image description here

8
votes

Make sure you have Microsoft.EntityFrameworkCore.Tools package installed. This package defines the PMC commands for EF Core.

1
votes
  1. Cleaned the project
  2. Closed down Visual Studio
  3. Reopen and ran the "add-migration" again and the error disappeared
1
votes

In My Case, I installed Microsoft.EntityFrameworkCore.SqlServer