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"); } } }