EF core not creating tables on migrate method

asp.net-core c# entity-framework-core sql-server


Hey I just started using EF core and everything works fine. I call the the context.Database.Migrate() method and it creates a database. But even though my context object has a DBSet<T>, it doesn't create any tables except for the migration history.

Can anyone help me with this issue?

5/20/2018 4:19:34 PM

Accepted Answer

context.Database.Migrate() in itself does not generate migrations. Instead, it processes your created migrations.

For each database change, you should call Add-Migration {sensibleName}.

Your startup class would continue to call context.Database.Migrate() which will check your database and process any outstanding migrations.

For example once you have created your database, a general rule is to call Add-Migration Initial. Calling context.Database.Migrate() once will check your database exists, create it if not, check if Initial migration is applied, and apply it if not.

If you then call Add-Migration SmallChange, the same will happen on next startup, similar to the following:

  1. Does database exist? Yes
  2. Has migration Initial been applied? Yes
  3. Has migration SmallChange been applied? No
  4. Apply Migration SmallChange

Your first migration should look a little something like this:

public partial class Initial : Migration
    protected override void Up(MigrationBuilder migrationBuilder)
            name: "HelloWorld",
            columns: table => new
                Id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                MyString = table.Column<string>(nullable: true),

If your migration doesn't look like that, it may be that your DbContext isn't correctly configured. If your tables still aren't being applied, try running the database update from your Package Manager Console and see what exactly is happening with Update-Database -Verbose

5/20/2018 4:27:23 PM

Popular Answer

I did the follow and works:

  1. Erase Migrations Folder in my APP project.
  2. Standing on DataAccess project, where I got persistency declared (EF, Dbcontext, etc) 2.1 Add-Migration Initial . 2.2 Update DataBase from console.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow