hyperar
hyperar

Reputation: 47

Entity Framework apply migrations at runtime

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?

Upvotes: 1

Views: 2819

Answers (1)

Rick
Rick

Reputation: 121

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)
        {
        }

Upvotes: 2

Related Questions