1

i'm creating a C# WinForms application which uses Entity Framework Code First and it is set to create the database if it doesn't exists.

Since the app is not distributed with a database, it creates it when it's needed, so i need to find a way to detect which migrations need to be applied for each case when i release a new version of the app.

How can i detect and apply needed migrations at runtime?

2
  • 1
    have you checked this SO QA? especially this link that uses DB Migrator. It still available in EF 6 so, it might relevant to your question.. Commented Dec 19, 2016 at 1:46
  • This seems to be exactly what i was looking for, Thanks!! Commented Dec 19, 2016 at 1:49

1 Answer 1

2

try this Initializer:System.Data.Entity.MigrateDatabaseToLatestVersion,it will update your database(no delete db,no delete data),just update entity changed.

Database.SetInitializer(new MigrateDatabaseToLatestVersion<T, DbMigrationsConfiguration<T>>()); try { using (var ctx = new T()) { ctx.Database.Initialize(true); } } catch (Exception e) { } 
Sign up to request clarification or add additional context in comments.

3 Comments

I used a variation of what you proposed. Database.SetInitializer<ObjectContext>( new MigrateDatabaseToLatestVersion<ObjectContext, Configuration>());
Just a very slight nitpick.. empty catch blocks are no end of trouble...
@gburton it's actually just the beginning of another one.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.