Entity Framework 7 migration not creating tables
You need to set up the entity in your database context first. At the very least, you would need to do this:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<NavigationMenu>();
}
The problem with your migrations was a bit hidden in your project layout. So what you have is a JobSight.DAL
project that contains the entities and the database context. And then you have a project JobSight.WebUI
which is the actual ASP project containing the Startup.cs
with the database setup.
This is causing problems because by default EF will just assume to find everything in the current assembly. So if you are launching the ef
command from your web project, it will create the migrations in there even if the context is in another project. But when you’re then trying to apply the migration, EF will not find it since it will only look in the context’s project.
So to fix this, you need to create the migrations in the DAL
project. You can do that by specifying the project when you call the ef
command:
dnx ef migrations add Example -p JobSight.DAL
You can verify that this worked by running dnx ef migrations list
afterwards. This should now return the Example
migration; previously, that command didn’t return anything: It could not find a migration which is the reason why the update
command only said Done
(without applying the migration) and the database wasn’t created. So if you now get the migration there, you can then apply it using:
dnx ef database update
Note that since the migration is now created in the DAL project, you need to add a reference to EntityFramework.MicrosoftSqlServer
there, otherwise the project will not compile. You need to do that before you can run the list
command above.
Finally, for some more information about this, see this issue.
Although this is not the answer to the original question, I post my answer here because it might help someone who has a similar problem. My problem was also that the tables were not created, but dotnet ef migrations add InitialCreate
did create 3 .cs files in the Migrations folder. But dotnet ef database update
only created the MigrationsHistory table and dotnet ef migrations list
did not return any migrations.
It turned out that the problem was that the Migrations folder was excluded from the Visual Studio project. Once I included it again, everything worked fine.