Reputation: 926
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?
Upvotes: 20
Views: 38396
Reputation: 2333
If you don't have a database, nor any migrations created (no Migrations
folder in your project), then calling Migrate
will create the database without any tables.
If you want the tables to be created as well, one option is to call EnsureCreated
instead of Migrate
:
context.Database.EnsureCreated()
This will create the database and the tables. However, the generated database won't be updatable later using migrations. So this should only be used in limited cases, such as when testing or prototyping.
Upvotes: 2
Reputation: 155
I will supplement the answer that is marked as correct. In my case, the migration did not work, although everything was as in the answer above. The EF tools update helped me: dotnet tool update --global dotnet-ef
Upvotes: 4
Reputation: 558
So I fought with this for a long time and I couldn't figure it out. My migrations are in the application project while my DbContext was in another.
When creating the options for the context you want to apply the migrations to you need to specify the MigrationAssembly.
serviceCollection.AddDbContext<YourContext>(options => options.UseSqlServer(_configuration.GetConnectionString("ConnectionString"), b => b.MigrationsAssembly("Your.Assembly")));
Upvotes: 14
Reputation: 59
I did the follow and works:
Upvotes: 5
Reputation: 22019
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:
Your first migration should look a little something like this:
public partial class Initial : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
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
Upvotes: 30