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:
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):
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.
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'.
Has anyone came across this before? I explicitly tried adding the EF packages but that didn't work either!
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
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