具有多个数据库服务器的ServiceStack OrmLite [英] ServiceStack OrmLite with multiple Database Servers
问题描述
我正在围绕servicestack框架构建一个应用程序,并且需要能够访问Oracle和MS Sql Server中的数据.使用ORMLite是否可以实现此功能,看来我只能为该应用设置一种方言,还是错过了什么?
I'm building an app around the servicestack framework and need to be able to access data in both Oracle and MS Sql Server. Is this possible using ORMLite, it seems that I can only set a single dialect for the App or have I missed something?
推荐答案
是的,OrmLiteConnectionFactory
中已经内置了对此功能的支持,请参见OrmLite项目主页上的Master SQLServer + Sqlite分片示例.
Yes it is possible and support for this is already built into the OrmLiteConnectionFactory
, see the Master SQLServer + Sqlite shard example on OrmLite's project home page.
基本上,您应该先通过以下方式注册默认(或主服务器)连接:
Basically you would register your default (or master) connection first with:
var dbFactory = new OrmLiteConnectionFactory(
"Data Source=host;Initial Catalog=RobotsMaster;Integrated Security=SSPI",
SqlServerDialect.Provider);
然后,您将为每个您希望支持的其他连接注册一个命名连接,例如:
Then you would register a named connection for every other connection you wish to support, e.g:
dbFactory.RegisterConnection("shard-1",
"~/App_Data/{0}.sqlite".Fmt(shardId).MapAbsolutePath(),
SqliteDialect.Provider);
配置完成后,在不指定名称的情况下打开连接将打开与默认数据库的连接,例如:
Once that's configured, opening a connection without specifying a name will open a connection to the default database, e.g:
using (IDbConnection db = dbFactory.OpenDbConnection()) { ... } //Default DB
虽然您可以指定一个名称,以使用其他提供程序打开与数据库的命名连接,例如:
Whilst you can specify a name to open up a named connection to a db with a different provider, e.g:
using (var dbShard = dbFactory.OpenDbConnection("shard-1")) { ... } //Named DB
手动使用其他方言提供者
每个方言提供程序中都包含不同RDBMS之间的SQL提供程序实现之间的差异.因此,如果要针对特定的ADO.NET提供程序实现使用OrmLite的便捷扩展方法,则只需分配要使用的ThreadStatic DialectProvider,例如:
Manually use different Dialect Providers
The differences between the SQL Provider implementations between different RDBMS's are contained within each dialect provider. So if you want to use OrmLite's convenience extension methods against an specific ADO.NET provider implementation you just need to assign the ThreadStatic DialectProvider you wish to use, e.g:
OrmLiteConfig.DialectProvider = SqlServerDialect.Provider;
var dbConn = new SqlConnection(SqlServerConnString);
dbConn.Select<Table>(); //All db access now uses the above dialect provider
这基本上是OrmLiteConnectionFactory中的RegisterConnection
在后台自动为您执行的所有操作.
This is essentially all what RegisterConnection
in OrmLiteConnectionFactory automatically does behind the scenes for you.
到目前为止,此处提供所有OrmLite的方言提供者供参考:
For reference here are all the dialect providers for OrmLite up to this point:
- SqlServerDialect.Provider
- SqliteDialect.Provider(提供不同的32/64和Mono展示)
- MySqlDialect.Provider
- PostgreSqlDialect.Provider
- OracleDialect.Provider
- FirebirdDialect.Provider
这篇关于具有多个数据库服务器的ServiceStack OrmLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!