I have two Azure Mobile Services (.NET backend) which share the same Azure Database. Let say Service "X" and "Y". The database is created by service "X" (when it ran for the first time) and created tables "TA" with schema name "X". Then I ran service "Y" which created the same tables "TA" and "TB" in the same database but with schema name "Y".
Now I want to make service "Y" to use schema "X" to make sure both services use the same data. Inside the "TADataController" I changed the code to:
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
// MyContext context = new MyContext(Services.Settings.Name.Replace('-', '_'));
YContext context = new YContext("X");
DomainManager = new EntityDomainManager<ADataController>(context, Request, Services);
}
Also in the "YContext" class
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//string schema = ServiceSettingsDictionary.GetSchemaName();
//if (!string.IsNullOrEmpty(schema))
// modelBuilder.HasDefaultSchema(schema);
modelBuilder.HasDefaultSchema("X");
}
but when I try to access the data inside "TADataController" using Query(), I get a SqlException with message "The specified schema name "X" either does not exist or you do not have permission to use it." I doubt that permission would be the issue as both services use the same Azure Database account and also clearly the schema exists as my other service uses it! so I cannot figure out what the problem is.
I also tried:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var entityConfig = modelBuilder.Entity<UserProfileData>();
entityConfig.ToTable("UserProfileDatas", "X");
}
but the same exception was raised. There are some information on the web for transferring data to another schema which could be helpful if I wanted to transfer my data to service "Y". But I want to use both services "X" and "Y" on a shared database.
UPDATE: I realized that ServiceSettingsDictionary.GetSchemaName() used in the OnModelCreating returns the name of the schema based on the configuration, so I uploaded the original codes:
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
MyContext context = new MyContext(Services.Settings.Name.Replace('-', '_'));
DomainManager = new EntityDomainManager<ADataController>(context, Request, Services);
}
and
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
string schema = ServiceSettingsDictionary.GetSchemaName();
if (!string.IsNullOrEmpty(schema))
modelBuilder.HasDefaultSchema(schema);
}
then in the management portal, I added one item to Configuration\App Settings with Key=MS_TableSchema and Value=X. Then debugged the code again, I realized that ServiceSettingsDictionary.GetSchemaName() returns "X" as I wanted, but again the controllers throw a SqlException with message "The specified schema name "X" either does not exist or you do not have permission to use it." when I use the Query() method.