简体   繁体   English

两个Azure移动服务(.NET后端)共享同一个数据库

[英]Two Azure Mobile Services (.NET backend) sharing the same Database

I have two Azure Mobile Services (.NET backend) which share the same Azure Database. 我有两个共享相同Azure数据库的Azure移动服务(.NET后端)。 Let say Service "X" and "Y". 假设服务“X”和“Y”。 The database is created by service "X" (when it ran for the first time) and created tables "TA" with schema name "X". 数据库由服务“X”(当它第一次运行时)创建,并创建具有模式名称“X”的表“TA”。 Then I ran service "Y" which created the same tables "TA" and "TB" in the same database but with schema name "Y". 然后我运行服务“Y”,它在同一个数据库中创建了相同的表“TA”和“TB”,但模式名称为“Y”。

Now I want to make service "Y" to use schema "X" to make sure both services use the same data. 现在我想让服务“Y”使用模式“X”来确保两个服务使用相同的数据。 Inside the "TADataController" I changed the code to: 在“TADataController”内部,我将代码更改为:

    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 也在“YContext”类中

    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." 但是当我尝试使用Query()访问“TADataController”中的数据时,我得到一个带有消息的SqlException“指定的模式名称”X“要么不存在,要么您没有权限使用它”。 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! 我怀疑权限是否会成为问题,因为两个服务都使用相同的Azure数据库帐户,而且当我的其他服务使用它时,显然存在模式! 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". Web上有一些信息用于将数据传输到另一个模式,如果我想将数据传输到服务“Y”,这可能会有所帮助。 But I want to use both services "X" and "Y" on a shared database. 但我想在共享数据库上使用“X”和“Y”服务。

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: 更新:我意识到OnModelCreating中使用的ServiceSettingsDictionary.GetSchemaName()根据配置返回架构的名称,所以我上传了原始代码:

    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. 然后在管理门户中,我使用Key = MS_TableSchema和Value = X向Configuration \\ App Settings添加了一个项目。 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." 然后再次调试代码,我意识到ServiceSettingsDictionary.GetSchemaName()按我的意愿返回“X”,但是控制器再次抛出一个SqlException,消息“指定的模式名称”X“要么不存在,要么你没有权限用它。” when I use the Query() method. 当我使用Query()方法时。

Just so that I understand -- you want your two services to access the same set of tables in the same database? 只是为了让我理解 - 你希望你的两个服务在同一个数据库中访问同一组表吗? Not just using the same database but the same tables? 不只是使用相同的数据库,而是使用相同的表格?

You can easily reuse the same database but we set each mobile service using it up with a separate schema and permissions to only access that schema so that two services won't inadvertently interfere. 您可以轻松地重复使用相同的数据库,但我们将每个移动服务设置为使用单独的架构和权限,以便仅访问该架构,以便两个服务不会无意中发生干扰。

However, it sounds like you want them to access the same tables, right? 但是,听起来你希望他们访问相同的表,对吧? This means that in addition to changing the schema in your service you also need to set the permissions for the mobile user in the DB to access that schema. 这意味着除了更改服务中的架构之外,还需要为DB中的移动用户设置权限以访问该架构。

You can get the user using the kudu site under the Environment tab (look for the MS_TableConnectionString connection string): 您可以使用环境选项卡下的kudu站点获取用户(查找MS_TableConnectionString连接字符串):

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

And you can set the permissions for that user using the grant command -- you can see an example her: 您可以使用grant命令为该用户设置权限 - 您可以看到她的示例:

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

Hope this helps! 希望这可以帮助!

Henrik 亨里克

When a mobile service named 'MSName' is created, a schema with the name 'MSName' is defined in the database, and a user is created with permission to access that schema only. 创建名为“MSName”的移动服务时,将在数据库中定义名为“MSName”的模式,并创建仅具有访问该模式的权限的用户。 That is the user in the default connection string which the mobile service will use when connecting to the database in the runtime. 这是默认连接字符串中的用户,移动服务在运行时连接到数据库时将使用该字符串。 So if you have a service 'X' and try from it to access the service 'Y', the user it's using will not have permissions to do so. 因此,如果您有服务'X'并尝试使用它来访问服务'Y',那么它所使用的用户将无权这样做。

There are a few options you can do to solve that. 您可以采取一些方法来解决这个问题。 One is to find the user for service 'Y' (using one of the SQL server management tools) and grant it access to the schema for 'X'. 一种是找到服务'Y'的用户(使用SQL服务器管理工​​具之一)并授予其访问'X'模式的权限。 Another option is to, when creating the context in the mobile service Y not to use the default connection string ("Name=MS_TableConnectionString"), but a connection string which uses the user for 'X'. 另一个选择是,当在移动服务Y中创建上下文时不使用默认连接字符串(“Name = MS_TableConnectionString”),而是使用用户“X”的连接字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM