从应用程序更改连接字符串中的数据库 [英] Change database in connectionstring from app

查看:30
本文介绍了从应用程序更改连接字符串中的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 Xamarin,我想在其中查询 Azure SQL Server 上的一个数据库,并且关于返回的结果我想查询某些数据库的 X 个数字之一.

I have created a Xamarin where I want to query one database on a Azure SQL Server and regarding which result I get back I want to query one of X numbers of certain databases.

到目前为止,我已经能够创建两个不同的 API,其中第一个 API 从第一个数据库中获取信息.

So far I have been able to create two different APIs where the first API gets information from the first database.

并且我已经硬编码(在 TableController 中)以使用一个特定的数据库(在同一个 SQL Server 中).

And I have hardcoded (in the TableController) to use one specific database (in the same SQL Server).

  string dbString = "database2";
  myContextClass context = new myContextClass(dbString);

这就像一个魅力.然而.我希望能够从我的应用程序中传递我想要连接到的数据库.

This works like a charm. However. I would like to be able to pass which database I want to connect to from my app.

即在调用我的移动服务时,我所做的就是:

I.e. when calling my mobileservice all I do is this:

 this.client = new MobileServiceClient(
                Constants.DatabaseURL);

我可以在这个方法调用中添加一些东西来在控制器中设置数据库连接字符串吗?

Is there something I can add to this methodcall that will set the database connectionstring in the controller?

推荐答案

我认为您需要 2 个 dbContext 实例.这是控制器中如何初始化 1 个 dbContext 的示例.因此,我想,您可以使用要指定所需数据库的参数,并使用主控制器或调用另一个用其他 dbContext 初始化的控制器.(或尝试用不同的上下文覆盖 DomainManager)

I think you need 2 dbContext instances. Here is an example in the controller how to initialize 1 dbContext. So, I guess, you could have the parameter you want to specify which db you want, and use the main controller or call another controller which is initialized with the other dbContext. (or try overwriting the DomainManager with different context)

 public class TodoItemController : TableController<TodoItem>
    {
        protected override void Initialize(HttpControllerContext controllerContext)
        {
            base.Initialize(controllerContext);
            AppContext context = new AppContext();
            DomainManager = new EntityDomainManager<TodoItem>(context, Request);
        }

        // GET tables/TodoItem
        public IQueryable<TodoItem> GetAllTodoItems()
        {
            return Query();
        }

然后你有 2 个这样的 dbContext 实例:

Then you have 2 dbContext instances like this:

public class AppContext : DbContext
    {
        private const string connectionStringName = "Name=MS_TableConnectionString";

        public NeptuneAppContext() : base(connectionStringName)
        {
        } 

您可以在其中指定不同的 connectionStrings.希望有帮助

where you can specify different connectionStrings. Hope it helps

这篇关于从应用程序更改连接字符串中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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