u936293
u936293

Reputation: 16264

What triggers code first migration?

How does the command Enable-Migration know whether to generate the migration code files?

I have the following in a project using the standard MVC 5 template in Visual Studio:

public class UsersContext : IdentityDbContext<ExtendedUser>
{
    public DbSet<Log> Logs { get; set; }

    public UsersContext() : base() { }

    public UsersContext(string ConnectionString) : base(ConnectionString) { }
}

public class Log
{
    public Int32 Id { get; set; }
    public DateTime Time { get; set; }
    public string Message { get; set; }
}

When I run Enable-Migrations, the Migrations folder is created but there is only the Configuration.cs file. There is no code generated to handle the creation of the Logs table.

A second related question is: do I have to run Add-Migration every time there are model changes after the previous Add-Migration was run?

Upvotes: 1

Views: 717

Answers (2)

user3083619
user3083619

Reputation:

Take note that Enable-Migrations is not the same as Automatic migrations.

Automatic migrations will, if enabled, automatically create and run your migration scripts during runtime, which means that you do not need to run the "add-migrations" and "update-database" commands manually every time your model is updated.

Upvotes: 1

Agent Shark
Agent Shark

Reputation: 525

When you run Enable-Migrations, it searches the project for any DBContexts and configures that project to use migrations.

Do I have to run Add-Migration every time there are model changes after the previous Add-Migration was run?

Yes. Running Add-Mingration ... creates files that details your model changes.

Running Update-Database pushes your changes to the database. Update-Database -Script (before running just update-database) generates an SQL script of your model changes.

After you update the database, if you make more model changes, you would add another migration. And the cycle goes on...

See Tips for Entity Framework Migrations

Upvotes: 1

Related Questions