开关`ConnectionStrings`本地和远程之间,LINQ到SQL [英] Switch `ConnectionStrings` between local and remote with Linq to SQL

查看:133
本文介绍了开关`ConnectionStrings`本地和远程之间,LINQ到SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,通过远程我的意思是我们网络上的中心专用的SQL Server。由地方我指的是本地SQL防爆preSS安装。

First, by Remote I mean a central dedicated SQL Server on our Network. By Local I mean a local SQL Express install.

我的情况是唯一的房子。无云服务,外部网站等我的应用程序允许用户离线工作,但为了减少交通和其他一些问题,以及我想以允许它们直接连接到中央服务器或者自动每当它可用,并且/或者当他们选择设置中央服务器的设置。

My situation is in house only. No cloud services, external sites, etc. My app allows the user to work disconnected but in order to minimize traffic and a few other issues as well I would like to allow them to connect straight to the central server either automatically whenever it is available and/or when they set a setting choosing Central Server.

是设置是相当简单的。给大家一个本地连接字符串是像这样 - >

Are setup is quite simple. A local connection string for everyone is like so -->

数据源= \ SQLEX $ P $干燥综合征;初始目录= MemberCenteredPlan;集成安全性=真

和像这样一个中央SQL连接字符串 - >

and a Central SQL connection string like so -->

数据源= CentralSQL;初始目录= MemberCenteredPlan;集成安全性=真

另外,我的数据是从我的UI一个单独项目,因此我有困难,搞清楚如何来访问数据层的UI层的设置文件。

Also, my Data is in a seperate project from my UI, as such I was having difficulties figuring out how to access the Settings file from the Data layer for the UI layer.

我是否应该添加一个参数的所有方法和IsOnline变量传递给他们呢?看似重复,但如果我知道一个更好的办法,我不会张贴在首位。

Should I add a parameter to all methods and pass a IsOnline variable to them? Seems repetitive but if I knew a better way I wouldn't be posting in the first place.

感谢您的帮助!

<一个href="http://stackoverflow.com/questions/966123/linq-to-sql-best-way-to-switch-between-test-dev-db">This是一个非常类似的帖子,但我不知道是否该意见是不同的,当我想以一个本地数据库和运行在远程数据库之间进行切换。

This is a very similar post but I wonder if the advice is different when I want to switch between a Local DB and a Remote DB during runtime.

推荐答案

一种选择我使用的是建立在这种情况下,在DataContext的方法(在部分类):

One option I use is to create a method on the DataContext (in a partial class) in this case:

public static DataContext New
{
  get
  {
    var cs = IsConnected ? CentralConnectionString : LocalConnectionString;
    return new DataContext(cs);
  }
}

您可以牛肉最多的切换逻辑,但是你想要的自动切换。然后在code基准,只是用这样的格式:

You could beef up that switch logic however you wanted for the automatic switching. Then to reference in code, just use a format like this:

var DB = DataContext.New;
var result = from a in DB.....

它使你的DataContext创建逻辑在一个地方,它是否适合你的需求,我觉得它简化了的东西无处不在。

It keeps your datacontext creation logic in one spot, if it suits your needs, I find it simplifies things everywhere.

这篇关于开关`ConnectionStrings`本地和远程之间,LINQ到SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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