Milan Nankov – Blog

Azure Mobile Services – Drop or Create Database 2014/07/07

When you are initially building an app or you are simply experimenting you find yourself in a situation where the database schema is updated quite frequently. If you are familiar with Entity Framework Code First you know that you can use DropCreateDatabaseIfModelChanges or DropCreateDatabaseAlways initializers to help you with keeping your schema updated. When you are using Azure Mobile Services those initializers will not work because your mobile service does not have permissions to drop the database. If you try to use DropCreateDatabaseIfModelChanges or DropCreateDatabaseAlways you will get an error similar to this:

“This operation requires a connection to the ‘master’ database. Unable to create a connection to the ‘master’ database because the original database connection has been opened and credentials have been removed from the connection string…”

The latest set of updates to Azure Mobile Services introduced two new initializers (ClearDatabaseSchemaIfModelChanges and ClearDatabaseSchemaAlways) that resolve this issue. As their names suggest, those initializers will only modify the database objects within a particular schema instead of modifying the whole database. More information about the latest update can be found here. Keep in mind that those initializers are suitable for the early stages of app development where you do not have to preserve existing data.