Entity Framework 7 transaction does not create tables

I am working with my first project using Entity Framework 7 and connecting to SQL Server, where the database has already been created, but there are no tables yet. I created my DbContext and created a class, and then set the DbSet<> inside my context. I ran the commands to enable the migration and create the first migration, and then the rand command to update the database. Everything looked fine, no errors occurred, but when I look at the database, the EFMigraitonsHistory table was created. When I look at the class that was created for the initial migration, it is essentially empty. What am I doing wrong?

The commands that I run are:

 dnvm install latest -r coreclr dnx ef migrations add MyFirstMigration dnx ef database update 

Context:

 namespace JobSight.DAL { public class JobSightDBContext : DbContext { public DbSet<NavigationMenu> NavigationMenu { get; set; } } } 

Table class:

 namespace JobSight.DAL { public class NavigationMenu { [Required, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Int16 ID { get; set; } public string ControllerName { get; set; } public string ActionName { get; set; } public string ExternalURL { get; set; } public string Title { get; set; } public Int16? ParentID { get; set; } public virtual NavigationMenu Parent { get; set; } } } 

Startup.cs:

  public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddEntityFramework() .AddSqlServer() .AddDbContext<JobSightDBContext>(options => { options.UseSqlServer(Configuration["Data:JobSightDatabase:ConnectionString"]); }); } 

Class of initial migration (auto-generated by EF):

 namespace JobSight.WebUI.Migrations { public partial class Initial : Migration { protected override void Up(MigrationBuilder migrationBuilder) { } protected override void Down(MigrationBuilder migrationBuilder) { } } } 

Edit: After Poke suggested that this is my new automatic migration. However, the table is still not created at the database level.

 namespace JobSight.WebUI.Migrations { public partial class MyFirstMigration : Migration { protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( name: "NavigationMenu", columns: table => new { ID = table.Column<short>(nullable: false) .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn), ActionName = table.Column<string>(nullable: true), ControllerName = table.Column<string>(nullable: true), ExternalURL = table.Column<string>(nullable: true), ParentID = table.Column<short>(nullable: true), Title = table.Column<string>(nullable: true) }, constraints: table => { table.PrimaryKey("PK_NavigationMenu", x => x.ID); table.ForeignKey( name: "FK_NavigationMenu_NavigationMenu_ParentID", column: x => x.ParentID, principalTable: "NavigationMenu", principalColumn: "ID", onDelete: ReferentialAction.Restrict); }); } protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable("NavigationMenu"); } } } 
+7
c # asp.net-core entity-framework-core
source share
3 answers

First you need to configure the object in the context of the database. At least you will 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, you have a JobSight.DAL project that contains the entities and context of the database. And then you have a JobSight.WebUI project, which is the actual ASP project containing Startup.cs with the database setup.

This causes problems because by default, EF simply expects to find everything in the current assembly. Therefore, if you run the ef command from your web project, it will create migrations there, even if the context is in another project. But when you try to apply migration, EF will not find it, because it will only look in the context of the project.

So, to fix this, you need to create migrations in the DAL project. This can be done by specifying the project when you invoke the ef command:

 dnx ef migrations add Example -p JobSight.DAL 

You can verify that this worked after running the dnx ef migrations list . Now this should return the migration Example ; previously this command returned nothing: it was not possible to find a migration, due to which the update command indicated only Done (without applying migration), and the database was not created. Therefore, if you are now getting a migration, you can apply it using:

 dnx ef database update 

Please note that since the migration is now created in the DAL project, you need to add a link to EntityFramework.MicrosoftSqlServer , otherwise the project will not compile. You need to do this before you can execute the list command above.

Finally, for more information about this, see this issue .

+15
source share

Although this is not the answer to the original question, I am posting my answer here because it can help someone who has a similar problem. My problem was also that the tables were not created, but dotnet ef migrations add InitialCreate created 3.cs files in the Migrations folder. But dotnet ef database update created the MigrationsHistory table and dotnet ef migrations list did not return any migrations.

It turned out that the problem was that the Migration folder was excluded from the Visual Studio project. As soon as I turned it on again, everything worked fine.

+2
source share

I had the same problem and this is how I solved it

  1. I deleted my database in SQL
  2. I changed the name that I used for the previous migration. I changed from "Add-Migration InitialCreate" to "Add-Migration NewName"
0
source share

All Articles