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?
1 Answers
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
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
This is essentially all what RegisterConnection
in OrmLiteConnectionFactory automatically does behind the scenes for you.
For reference here are all the dialect providers for OrmLite up to this point:
- SqlServerDialect.Provider
- SqliteDialect.Provider (different 32/64 and Mono impls available)
- MySqlDialect.Provider
- PostgreSqlDialect.Provider
- OracleDialect.Provider
- FirebirdDialect.Provider

- 141,670
- 29
- 246
- 390
-
Brilliant, thanks for the quick response. I hadn't spotted that within the named connection I could specify the dialect. I'll give it a go. – MikeT Dec 04 '12 at 20:01
-
Please don't post code in comments. Create a new question and show exactly what you're trying to do and the full exception details. – mythz Dec 04 '12 at 21:26
-
Weird thing that happened to me regarding using two connections in the same factory that happen to use different dialectproviders: If I opened a transaction on the secondary connection and committed it but neglected to dispose of the transaction, subsequent usage of the first connection would utilize the dialect of the second. Properly disposing of transactions resolved my issue. – Chris Ray Oct 07 '13 at 21:11