Search code examples
azureversion-controlef-code-firstbitbucketazure-deployment

Azure deployment from source control - code first migration


Can you help me how I can enable code first migration in Azure when it is deployed from a source control - Bitbucket?

As it is not directly pushed to Azure, so I cannot enable Code First Migration profile in publishing.


Solution

  • You want it to automatically upgrade the database (by applying any pending migrations) when the application launches, right?

    You can do this by registering the MigrateDatabaseToLatestVersion database initializer. A database initializer simply contains some logic that is used to make sure the database is setup correctly. This logic is run the first time the context is used within the application process (AppDomain).

    static void Main(string[] args) 
            { 
                Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDatabaseContext, Configuration>()); 
    ...