Two Azure Mobile Services (.NET backend) using the same database

I have two Azure Mobile Services (.NET backend) that have the same Azure database. Let them say “Service” “X” and “Y”. The database is created by the "X" service (when it is launched for the first time) and created the "TA" tables with the schema name "X". Then I started the service “Y”, which created the same tables “TA” and “TB” in the same database, but with the schema name “Y”.

Now I want to make the service "Y" to use the scheme "X", so that 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 class "YContext"

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
            //string schema = ServiceSettingsDictionary.GetSchemaName();
            //if (!string.IsNullOrEmpty(schema))
            //  modelBuilder.HasDefaultSchema(schema);
            modelBuilder.HasDefaultSchema("X");
    }

"TADataController" Query(), SqlException " " X " , ". , , Azure Database, , , ! , .

:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        var entityConfig = modelBuilder.Entity<UserProfileData>();
        entityConfig.ToTable("UserProfileDatas", "X");
    }

. , , "Y" . "X" "Y" .

UPDATE: , ServiceSettingsDictionary.GetSchemaName(), OnModelCreating, , :

    protected override void Initialize(HttpControllerContext controllerContext)
    {
        base.Initialize(controllerContext);
        MyContext context = new MyContext(Services.Settings.Name.Replace('-', '_'));
        DomainManager = new EntityDomainManager<ADataController>(context, Request, Services);
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
            string schema = ServiceSettingsDictionary.GetSchemaName();
            if (!string.IsNullOrEmpty(schema))
                modelBuilder.HasDefaultSchema(schema);
    }

Configuration\App Settings Key = MS_TableSchema Value = X. , , ServiceSettingsDictionary.GetSchemaName() "X" , , SqlException " " X " , ." Query().

+4
2

"MSName", "MSName", . , . , "X" "Y", , , .

, , . - "Y" ( SQL-) "X" . , Y ( "Name = MS_TableConnectionString" ), , "X" .

+2

- , ? , ?

, , , , .

, , , , ? , .

kudu "" ( MS_TableConnectionString):

https://<your service>.scm.azure-mobile.net/Env

grant - :

https://weblogs.asp.net/fredriknormen/database-migration-and-azure-mobile-service-adventure

, !

+2

Source: https://habr.com/ru/post/1542699/


All Articles