Reputation: 103
Database is made with code first using Fluent API. Migrations are enabled and init file is in migrations folder. Now we need to drop database and create it again with new definitions (mapping, required entities, etc.). Do I need to delete migrations folder, or some of migrations files to create database again? I am not sure if init file of migrations has any impact to OnModelCreating method of Fluent API. I tired to find the answer here on StackOverflow and also on Entity Framework Tutorial but I'm still not sure about it. Can someone explain me how exactly will be DB created with my scenario?
Upvotes: 0
Views: 759
Reputation: 2470
The key is in the Initializer class. In my case:
public class PricedNotesInitializer:CreateDatabaseIfNotExists<PricedNotesContext>
{
protected override void Seed(PricedNotesContext context)
{
}
}
Here my class extends CreateDatabaseIfNotExists
, but you're going to want, if I understand correctly, DropCreateDatabaseIfModelChanges
. So it would look like this:
public class PricedNotesInitializer:DropCreateDatabaseIfModelChanges<PricedNotesContext>
{
protected override void Seed(PricedNotesContext context)
{
}
}
Then what will happen when you do
Add-Migration NewMigrationName
Then EF will automatically generate the code as a migration in the Migrations folder.
To apply the migration, just type
Update-Database
Done! Any changes that you made in OnModelCreating
will be reflected in the migration (like changing keys, precision, etc). Moreover, anything written in that Seed method will be executing and your database will be recreated because we specified DropCreateDatabaseIfModelChanges
.
Hope this helps.
Upvotes: 1