WhiskerBiscuit
WhiskerBiscuit

Reputation: 5157

How can I call Entity Framework commands via code?

I can run the following from the package manager console

update-database -targetmigration:0 -force

How can I do the same thing via C#?

Upvotes: 1

Views: 87

Answers (1)

Chris Ballance
Chris Ballance

Reputation: 34347

There is a Database Initializer you can use to achieve the migration to latest version on startup (or better, the dbinitializer will kick in on first db access), the MigrateDatabaseToLatestVersion, you use it like that:

Database.SetInitializer<ObjectContext>(
    new MigrateDatabaseToLatestVersion<ObjectContext, Configuration>());

Source: https://stackoverflow.com/a/10850935/1551

You can also just set automigrations to true, and I'm sure you can configure force to happen also if necesary, though it probably will not by default.

Upvotes: 1

Related Questions