Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 814
Rob McCabe Avatar asked Sep 20 '17 10:09

Rob McCabe


Video Answer


2 Answers

  1. Cleaned the project
  2. Closed down Visual Studio
  3. Reopen and ran the "add-migration" again and the error disappeared
like image 171
tfa Avatar answered Oct 20 '22 09:10

tfa


In My Case, I installed Microsoft.EntityFrameworkCore.SqlServer

like image 42
maryam ghoreishi Avatar answered Oct 20 '22 10:10

maryam ghoreishi