--- title: Multi-nested database connections example --- The `OrmLiteConnectionFactory` class supports registering multiple named connections allows you to define all your db connections when registering it in your IOC, that can be accessed later using its registered name. ## Primary DB & Multi Shard DB Example A popular way of scaling RDBMS's is to create a Master / Shard setup where datasets for queries that span entire system are kept in the master database, whilst context-specific related data can be kept together in an isolated shard. This feature makes it trivial to maintain multiple separate db shards with a master database in a different RDBMS. Here's an (entire source code) sample of the code needed to define, and populate a Master/Shard setup. Sqlite can create DB shards on the fly so only the blank SqlServer master database needed to be created out-of-band: ### Sharding 1000 Robots into 10 Sqlite DB shards ```csharp public class MasterRecord { public Guid Id { get; set; } public int RobotId { get; set; } public string RobotName { get; set; } public DateTime? LastActivated { get; set; } } public class Robot { public int Id { get; set; } public string Name { get; set; } public bool IsActivated { get; set; } public long CellCount { get; set; } public DateTime CreatedDate { get; set; } } const int NoOfShards = 10; const int NoOfRobots = 1000; var dbFactory = new OrmLiteConnectionFactory( "Data Source=host;Initial Catalog=RobotsMaster;Integrated Security=SSPI", //Connection String SqlServerDialect.Provider); dbFactory.Run(db => db.CreateTable(overwrite:false)); NoOfShards.Times(i => { var namedShard = "robots-shard" + i; dbFactory.RegisterConnection(namedShard, $"~/App_Data/{shardId}.sqlite".MapAbsolutePath(), //Connection String SqliteDialect.Provider); dbFactory.OpenDbConnection(namedShard).Run(db => db.CreateTable(overwrite:false)); }); var newRobots = NoOfRobots.Times(i => //Create 1000 Robots new Robot { Id=i, Name="R2D"+i, CreatedDate=DateTime.UtcNow, CellCount=DateTime.Now.ToUnixTimeMs() % 100000 }); foreach (var newRobot in newRobots) { using (IDbConnection db = dbFactory.OpenDbConnection()) //Open Connection to Master DB { db.Insert(new MasterRecord { Id = Guid.NewGuid(), RobotId = newRobot.Id, RobotName = newRobot.Name }); using (IDbConnection robotShard = dbFactory.OpenDbConnection("robots-shard"+newRobot.Id % NoOfShards)) //Shard { robotShard.Insert(newRobot); } } } ``` Using the [SQLite Manager](https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/?src=search) Firefox extension we can peek at one of the created shards to see 100 Robots in each shard. This is the dump of `robots-shard0.sqlite`: ![Data dump of Robot Shard #1](/img/pages/ormlite/robots-shard0.sqlite.jpg) As expected each shard has every 10th robot inside.