我使用MVC和亚音速3.0.0.3,但我似乎无法牵制多个数据库连接的特定点。
am using MVC and Subsonic 3.0.0.3 but i cant seem to pin down a specific point for multiple database connection.
通常在正常.NET我有我的web.config文件中的2个字符串并且有一个数据库类为我的项目,该DB类中我会做这样的事情:
normally in normal i would have my 2 strings in the web.config file and have a database class for my project, within this db class i would do something like this:
try { conn.ConnectionString = server1; conn.Open(); } catch (MySqlException) { conn.ConnectionString = server2; conn.Open(); }我想牵制在亚音速的创建文件的一个地方,这样的事情将是最好的地方,也许一个最新的例子就如何实现这一目标。我用Google搜索等,但显示的例子是一个较旧的亚音速。
I am trying to pin down the one place in subsonic's created files where something like this would be best to place and maybe an up to date example on how to achieve it. I have googled etc but the examples shown are for an older subsonic.
非常感谢
推荐答案如果您在Context.tt在第35行看看,你会看到下面的code:
If you look in Context.tt at line 35 you'll see the following code:
public <#=DatabaseName#>DB() { DataProvider = ProviderFactory.GetProvider("<#=ConnectionStringName#>"); Init(); }这是那里的供应商越来越为你设置,所以如果你在Settings.ttinclude在第20行中的connectionStringName后添加BackupConnectionStringName变量,那么你应该能够检查你的连接工作和用户的后备如果不是。例如:
This is where the provider is getting setup for you so if you add a BackupConnectionStringName variable in Settings.ttinclude after the ConnectionStringName at line 20 then you should be able to check your connection is working and user your fallback if not. For example:
public <#=DatabaseName#>DB() { DataProvider = ProviderFactory.GetProvider("<#=ConnectionStringName#>"); Init(); try { DataProvider.CreateConnection(); } catch(SqlException) { DataProvider = ProviderFactory.GetProvider("<#=BackupConnectionStringName#>"); Init(); } }NB您可能需要做一些清理,以确保连接不剩通过创建连接开放。
NB You may need to do some clean up to make sure a connection is not left open by CreateConnection.
更多推荐
亚音速3.0.0.3多个数据库连接故障转移
发布评论