二Azure的移动服务(.NET后端)共享同一个数据库 [英] Two Azure Mobile Services (.NET backend) sharing the same Database

查看:180
本文介绍了二Azure的移动服务(.NET后端)共享同一个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个共享相同的Azure数据库中的两个的Azure移动服务(.NET后端)。让说服务的X和Y。该数据库由服务X创建(当它跑了第一次)和创建的表TA与架构名称X。然后我跑业务Y,这创造了在同一数据库但模式名Y。相同的表TA和TB

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".

现在我要让服务Y使用模式X,以确保这两种服务使用相同的数据。里面的TADataController我改变了code为:

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);
    }

另外在YContext类

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");
    }

但是当我尝试访问内部TADataController数据使用查询(),我得到的消息是指定的架构名称X抛出SQLException要么不存在,或者您没有使用它的权限。我怀疑该权限将是问题,因为这两种服务使用相同的Azure数据库帐户,也明确作为我的其他服务使用它的架构存在!让我想不通的是什么问题。

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.

我也试过:

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

但同样异常发生。有网络将数据传输到另一个模式,如果我想我的数据传送到服务Y,这可能是有帮助的一些信息。但我想一个共享数据库上使用这两种服务的X和Y。

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.

更新:
我意识到,在OnModelCreating使用ServiceSettingsDictionary.GetSchemaName()返回根据配置的架构的名字,所以我上传的原始codeS:

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);
    }

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

然后在管理门户,我增加了一个项目配置\\应用程序设置与重点= MS_TableSchema和Value = X。然后再次调试的code,我意识到ServiceSettingsDictionary.GetSchemaName()返回X因为我想,却又控制器抛出消息指定的架构名称X要么不存在,或者你做不成的SQLException必须使用它的权限。当我使用query()方法。

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.

推荐答案

在创建了一个名为MSName移动服务,名为'MSName的模式在数据库中定义的,用户使用权限创建只能访问该模式。这是连接在运行数据库时,移动服务将使用默认的连接字符串中的用户。所以,如果你有一个服务'X',并从中尝试访问服务'Y',它的使用将没有权限这么做的用户。

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. 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.

有可以做,以解决几个选项。一个是找到服务'Y'用户(使用的SQL Server管理工具之一),并授予其访问架构X。另一种选择是,在移动业务创建上下文当Y不使用它使用用户为X的默认连接字符串(名称= MS_TableConnectionString),但连接字符串。

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'. 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'.

这篇关于二Azure的移动服务(.NET后端)共享同一个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆