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?
Asked
Active
Viewed 732 times
0
-
i think here you can find your answer [Here](http://stackoverflow.com/a/16082497/4154016) – Usman lqbal Nov 08 '16 at 17:45
-
I used this tutorial a year or two back and really enjoyed it except that for whatever reason I think the config file setting for a custom initializer would not work. Anyways the section on initializers is here: http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-first.aspx. My issue was here: http://stackoverflow.com/questions/32384662/set-databaseinitializerfortype-in-config-file-for-custom-initializer-to-fire. Basically I wanted to define a reseeding for a 'Dev' environment different than a prod environment. – djangojazz Nov 08 '16 at 18:10
-
If you are using migrations and want to do a custom seeding method I went through that pain too. The problem is with a model change the concept of a 'data loss'. You should not have to delete your migrations as they will all stay unless you want to 'blank slate' everything and start over. The question you should ask yourself first is: "Do I have enable migrations to see a schema history in the first place?" If the answer is no, don't even go down that route. http://stackoverflow.com/questions/33446327/seeding-data-will-not-work-when-schema-changes-with-code-first-when-using-migrat – djangojazz Nov 08 '16 at 18:14
-
@usman your link is what I needed, thanks – Mony Nov 09 '16 at 07:49
-
@Mony you welcome . Don't forget to up-vote. – Usman lqbal Nov 09 '16 at 08:51
1 Answers
1
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.

Nissa
- 4,636
- 8
- 29
- 37

coolboyjules
- 2,300
- 4
- 22
- 42
-
My point is to create database like the current one never exist, so make a new connection string - to have 2 databases for same project (only one will be used of course), we just want to try which would be better for application. That's the reason why I don't know if I should or shouldn't delete migrations files. So maybe better question is when I want to create new DB, do I need to delete migrations? – Mony Nov 09 '16 at 07:42
-
Well no. If you just change your connection string to point to your new DB, you can jsut run 'Update-Database' and all of those migrations will be applied. It will have the exact same schema as your current database. Does that make sense? You don't need to delete anything. – coolboyjules Nov 09 '16 at 14:02
-
thanks for explanation, however I made changes before you wrote your comment and delete all migrations files as suggested in usman's link, DBs are not the same because changes in models, also thanks to your post I searched little bit more and finally found out meaning of `init` and `configuration` files of migrations and `OnModelCreating` method – Mony Nov 10 '16 at 07:58
-
No problem. Please accept the answer if it answered your question. – coolboyjules Nov 10 '16 at 13:50