public bool Update <TEntity> (IID entity) where TEntity : class
        {
            if (entity == null)
            {
                return(false);
            }

            if (entity.ID == 0)
            {
                return(Add <TEntity> (entity));
            }

            using (CSDB context = new CSDB(Connection))
            {
                TEntity origonal = GetSingle <TEntity>(entity.ID, context);

                MakeObjectsEqual(origonal, entity);

                context.SubmitChanges( );

                cache.AddToCache(typeof(TEntity), entity);

                return(true);
            }
        }
        public bool Delete <TEntity> (IID entity) where TEntity : class
        {
            if (Connection == null)
            {
                throw new Exception("No connection to database");
            }

            try
            {
                Type           baseType = typeof(TEntity);
                TableAttribute ta       = null;

                while (true)
                {
                    ta = (TableAttribute)Attribute.GetCustomAttribute(baseType, typeof(TableAttribute));

                    if (ta != null)
                    {
                        break;
                    }

                    baseType = baseType.BaseType;

                    if (baseType == null)
                    {
                        break;
                    }
                }

                if (ta == null)
                {
                    throw new ArgumentException("Failed to find Table of " + typeof(TEntity).ToString( ));
                }


                using (CSDB context = new CSDB(Connection))
                {
#if (DEBUG)
                    TextWriter log = new StringWriter();
                    context.Log = log;
#endif
                    TEntity t = GetSingle <TEntity> (entity.ID, context);

                    context.GetTable(baseType).DeleteOnSubmit(t);
                    context.SubmitChanges( );
#if (UseCache)
                    cache.DeleteItemFromCache(baseType, ((IID)t).ID);
#endif
                    return(true);
                }
            }
            catch (Exception ex)
            {
                ChampionshipSolutions.Diag.Diagnostics.LogLine("Error deleting entity from database of type " + typeof(TEntity).ToString( ));
                ChampionshipSolutions.Diag.Diagnostics.LogLine(ex.Message);
                return(false);
            }
        }
        private bool addRange <TEntity> (TEntity[] entities) where TEntity : class
        {
            if (Connection == null)
            {
                throw new Exception("No connection to database");
            }

            try
            {
                ArrayList newEntities = new ArrayList(entities.Count());
                TEntity   newEntity;

                // determine type here
                var type = typeof(TEntity);   //.GetType(); // entities.GetType();

                foreach (IID entity in entities)
                {
                    // create an object of the type
                    newEntity = (TEntity)Activator.CreateInstance(type);

                    MakeObjectsEqual(newEntity, entity);

                    newEntities.Add(newEntity);
                }


                using (CSDB context = new CSDB(Connection))
                {
                    TEntity[] array = (TEntity[])newEntities.ToArray();
                    context.GetTable <TEntity> ( ).InsertAllOnSubmit(array);
                    context.SubmitChanges( );
                }

                for (int i = 0; i < entities.Count( ); i++)
                {
                    ((IID)entities[i]).ID            = ((IID)newEntities[i]).ID;
                    ((IID)entities[i]).Discriminator = ((IID)newEntities[i]).Discriminator;
                    cache.AddToCache(type, (IID)entities[i]);
                }

                return(true);
            }
            catch (Exception ex)
            {
                ChampionshipSolutions.Diag.Diagnostics.LogLine("Error inserting range into database of type " + typeof(TEntity).ToString( ));
                ChampionshipSolutions.Diag.Diagnostics.LogLine(ex.Message);
                return(false);
            }
        }
        public bool DeleteRange <TEntity> (TEntity[] entities) where TEntity : class
        {
            if (Connection == null)
            {
                throw new Exception("No connection to database");
            }

            try
            {
                using (CSDB context = new CSDB(Connection))
                {
                    // retrieve entities from context

                    HashSet <TEntity> ToBeDeleted = new HashSet <TEntity>();

                    foreach (IID entity in entities)
                    {
                        TEntity t = GetSingle <TEntity> (entity.ID, context);
                        if (t != null)
                        {
                            ToBeDeleted.Add(t);
                        }
                    }
                    if (ToBeDeleted.Count == 0)
                    {
                        return(false);
                    }

                    context.GetTable <TEntity> ( ).DeleteAllOnSubmit(ToBeDeleted);
                    context.SubmitChanges( );

                    foreach (IID entity in entities)
                    {
                        cache.DeleteItemFromCache(typeof(TEntity), ((IID)entity).ID);
                    }

                    return(true);
                }
            }
            catch (Exception ex)
            {
                ChampionshipSolutions.Diag.Diagnostics.LogLine("Error deleting range from database of type " + typeof(TEntity).ToString( ));
                ChampionshipSolutions.Diag.Diagnostics.LogLine(ex.Message);
                return(false);
            }
        }
        private bool add <TEntity> (TEntity entity) where TEntity : class
        {
            if (Connection == null)
            {
                throw new Exception("No connection to database");
            }

            try
            {
                TEntity newEntity;

                // determine type here
                var type = entity.GetType();

                Type           baseType = typeof(TEntity);
                TableAttribute ta       = null;

                while (true)
                {
                    ta = (TableAttribute)Attribute.GetCustomAttribute(baseType, typeof(TableAttribute));

                    if (ta != null)
                    {
                        break;
                    }

                    baseType = baseType.BaseType;

                    if (baseType == null)
                    {
                        break;
                    }
                }

                if (ta == null)
                {
                    throw new ArgumentException("Failed to find Table of " + typeof(TEntity).ToString( ));
                }


                // create an object of the type
                newEntity = (TEntity)Activator.CreateInstance(type);

                MakeObjectsEqual(newEntity, entity);

                using (CSDB context = new CSDB(Connection))
                {
                    context.GetTable <TEntity> ( ).InsertOnSubmit(newEntity);
                    context.SubmitChanges( );
                }

                ((IID)entity).ID            = ((IID)newEntity).ID;
                ((IID)entity).Discriminator = ((IID)newEntity).Discriminator;

                cache.AddToCache(baseType, (IID)entity);

                return(true);
            }
            catch (Exception ex)
            {
                ChampionshipSolutions.Diag.Diagnostics.LogLine("Error inserting into database of type " + typeof(TEntity).ToString());
                ChampionshipSolutions.Diag.Diagnostics.LogLine(ex.Message);
                return(false);
            }
        }