public DistributedDataContext GetContext(byte bucket)
        {
            var ctx = new DistributedDataContext(_shards[bucket]);

            ctx.Configuration.AutoDetectChangesEnabled = false;
            ctx.Configuration.ProxyCreationEnabled     = false;
            return(ctx);
        }
        private void CheckShardsAndSetEpoch(DbMigrationsConfiguration config, bool updateMigrations)   //<DistributedDataContext>
        {
            var sortedShards   = _shards.OrderBy(kvp => kvp.Key).ToList();
            var numberOfShards = sortedShards.Count;

            if (numberOfShards > 254)
            {
                throw new ArgumentException("Too many shards!");
            }
            NumberOfShards  = (byte)numberOfShards;
            _writableShards = _shards.Keys.Except(_readOnlyShards).ToList();
            if (_writableShards.Count == 0)
            {
                throw new ApplicationException("No writable shards");
            }
            // one based
            var i = 0;

            foreach (var keyValuePair in sortedShards)
            {
                if (i != keyValuePair.Key)
                {
                    // TODO unit test
                    throw new ApplicationException("Wrong numbering of shards");
                }
                i++;
            }
            foreach (var key in sortedShards.Select(keyValuePair => keyValuePair.Key))
            {
                if (updateMigrations)
                {
                    DistributedDataContext.UpdateAutoMigrations(_shards[key], config);
                }
                //using (var ctx = GetContext(key)) {
                //    var two = ctx.Database.SqlQuery<int>("SELECT 1+1").SingleOrDefault(); // check DB engine is working
                //    if (two != 2) throw new ApplicationException("Shard " + key + " doesn't work");
                //}
            }
        }