1

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?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Regarding "...it searches the project for any DBContexts..." would the IdentityDbContext<ExtendedUser> in my code above qualify as a DBContext?
1

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.

2 Comments

Don't you still need to call the add-migration?
@Terry Not with automatic migrations. The migration is generated on the fly at run time, applied and discarded. No migration file is stored. It would be something like running Add-Migration, running Update-Database, and finally deleting the migration file you just created (probably with different effects on the migrations history table contents, but you get the idea).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.