public void RemoveShard(Shard shard)
        {
            using (var context = GetConnection(ConnectionString))
            {
                var dbShard = context.Shards.FirstOrDefault(x => x.ShardID == shard.PointerShardID);
                if (dbShard == null)
                {
                    throw new Exception("Shard not found.");
                }

                context.Shards.Remove(dbShard);
                context.SaveChanges();
            }
        }
        public Shard AddShardToShardSet(
            Shard shard, ShardSetConfig shardSetConfig)
        {
            using (var context = GetConnection(ConnectionString))
            {
                var dbShard = new Models.Shard
                {
                    Database =
                        context.Databases
                        .FirstOrDefault(
                            d =>
                            d.DatabaseName == shard.Catalog &&
                            d.Server.ServerName == shard.ServerInstanceName),
                    Description = shard.Description,
                    ShardSet    =
                        context.ShardSets
                        .FirstOrDefault(tg => tg.ShardSetID == shardSetConfig.ShardSetID)
                };

                if (dbShard.Database == null)
                {
                    dbShard.Database =
                        new Database
                    {
                        DatabaseName = shard.Catalog,
                        Server       =
                            context.Servers.FirstOrDefault(s => s.ServerName == shard.ServerInstanceName),
                    };

                    context.Databases.Add(dbShard.Database);
                }

                context.Shards.Add(dbShard);
                context.SaveChanges();
                shard.PointerShardID = dbShard.ShardID;
            }

            return(shard);
        }
        public Shard ModifyShard(Shard shard)
        {
            using (var context = GetConnection(ConnectionString))
            {
                var dbShard =
                    context.Shards.FirstOrDefault(ps => ps.ShardID == shard.PointerShardID);
                if (dbShard == null)
                {
                    throw new Exception("Shard not found.");
                }

                dbShard.Database =
                    context.Databases
                    .FirstOrDefault(
                        x =>
                        x.DatabaseName == shard.Catalog &&
                        x.Server.ServerName == shard.ServerInstanceName);
                if (dbShard.Database == null)
                {
                    dbShard.Database =
                        new Database
                    {
                        DatabaseName = shard.Catalog,
                        Server       =
                            context.Servers.FirstOrDefault(x => x.ServerName == shard.ServerInstanceName),
                    };

                    context.Databases.Add(dbShard.Database);
                }
                dbShard.Description = shard.Description;

                context.SaveChanges();
            }

            return(shard);
        }