Using Existing Windows Azure SQL Databases with Azure Mobile Services

I was one of the speakers today at the 6 Weeks of Azure bootcamp event in Sydney today. During the day, as well as speaking, we have an "unconference". There's a lot of discussion on whatever the attendees want to discuss.

One of the offline questions I got today though was about how to hook up Azure Mobile Services to an existing Windows Azure SQL Database rather than using the one that Mobile Services can auto-create. While getting that to work, we learned a few things:

Tables need to be created in a schema that has the same name as the Mobile Service. (ie: if I want a table called CurrentTasks, and my service is called TaskManager, I need to have my table called TaskManager.CurrentTasks.

Tables need to have a clustered primary key that is an integer and is called "id" (it appears case-sensitive in the app for some reason)

After you modify the mobile service to point to your database (or after you create a mobile service referencing the existing database), you need to add each table to the service by using the mobile services portal. (If you reference a table that doesn't exist, that's when it would also create the table)

The attendee had already created his tables in the dbo schema. We moved his tables by using:

ALTER SCHEMA servicename TRANSFER dbo.OldTableName;

It's also worth noting that Mobile Services isn't yet available in all datacenters. The attendee's database was in a different datacenter and it's important to try to avoid that, or the latency can be a big issue. He and another attendee were asking about the best ways to move the database to another datacentre. One option for this is to export a BACPAC of the database to Azure Storage, and to then import the database to the alternate server.

Unfortunately, even though CREATE DATABASE AS COPY OF works across servers within the same datacenter, it doesn't work for servers in different datacenters.