EntityFramework Core 2.0 - Add Migration error "The EntityFramework package is not installed"

asp.net-core c# ef-migrations entity-framework-core

Question

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

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

enter image description here

I am looking to utilise the Entity Framework Core (my understanding was that the All meta data 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 conext 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 came across this before? I explicitly tried adding the EF packages but that didn't work either!

1
7
2/1/2018 4:36:39 PM

Accepted Answer

I was able to get past this issue using the command-line-interface, by following article:

https://docs.microsoft.com/en-gb/aspnet/core/data/ef-mvc/migrations#introduction-to-migrations

2
9/25/2017 2:46:14 PM

Popular Answer

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



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow