Пример #1
0
        public void RemoveTable(Type tableType, List <Type> tableRemoved = null, bool remove = true)
        {
            if (tableRemoved == null)
            {
                tableRemoved = new List <Type>();
            }
            if (tableRemoved.Any(x => x == tableType))
            {
                return;
            }
            GlobalConfiguration.Log?.Info("Removig", tableType);
            tableRemoved.Insert(0, tableType);
            var props = DeepCloner.GetFastDeepClonerProperties(tableType);

            foreach (var prop in props.Where(x => (!x.IsInternalType || x.ContainAttribute <ForeignKey>()) && !x.ContainAttribute <ExcludeFromAbstract>()))
            {
                var key = prop.GetCustomAttribute <ForeignKey>();
                if (key != null && tableRemoved.Any(x => x == key.Type))
                {
                    continue;
                }
                if (key != null)
                {
                    RemoveTable(key.Type, tableRemoved, false);
                }
                else
                {
                    RemoveTable(prop.PropertyType.GetActualType(), tableRemoved, false);
                }
            }

            if (!remove)
            {
                return;
            }

            var tableData = ObjectColumns(tableType);

            if (!tableData.Rows.Any())
            {
                return;
            }
            var c = tableRemoved.Count;

            _repository.CreateTransaction();
            while (c > 0)
            {
                for (var i = tableRemoved.Count - 1; i >= 0; i--)
                {
                    try
                    {
                        var tType = tableRemoved[i];
                        if (ObjectColumns(tType).Rows.Any())
                        {
                            CachedObjectColumn.Remove(tType.FullName + _repository.DataBaseTypes.ToString());
                            if (Extension.CachedDataRecord.ContainsKey(tType))
                            {
                                Extension.CachedDataRecord.Remove(tType);
                            }
                            var tableName = tType.TableName();
                            _repository.ExecuteNonQuery(_repository.GetSqlCommand("DELETE FROM [" + tableName + "];"));
                            var cmd = _repository.GetSqlCommand("DROP TABLE [" + tableName + "];");
                            _repository.ExecuteNonQuery(cmd);
                            CachedObjectColumn.Remove(tType.FullName + _repository.DataBaseTypes.ToString());
                        }


                        c--;
                    }
                    catch (NpgsqlException ex)
                    {
                        _repository.Renew();
                    }
                    catch
                    {
                        // Ignore
                    }
                }
            }
        }
 public new void Remove(Attribute attr)
 {
     this.Remove(attr);
     ContainedAttributes.Remove(attr);
     ContainedAttributestypes.Remove(attr.GetType());
 }