Пример #1
0
        public void Delete <T>(IList <T> storedEntities) where T : class
        {
            ExecutionHelper helper = new ExecutionHelper(this.database);
            ITable <T>      table  = this.database.Tables.FindTable <T>();

            // Find relations
            // Do not add referred relations!
            RelationGroup relations =
                helper.FindRelations(table.Indexes, referred: false);

            // Find referring entities
            var referringEntities =
                helper.FindReferringEntities <T>(storedEntities, relations.Referring);

            // Delete invalid index records
            for (int i = 0; i < storedEntities.Count; i++)
            {
                T storedEntity = storedEntities[i];

                foreach (IIndex <T> index in table.Indexes)
                {
                    index.Delete(storedEntity);
                    log.Log.WriteIndexDelete(index, storedEntity);
                }
            }

            var cascadedRelations = relations.Referring.Where(x => x.Options.CascadedDeletion);

            foreach (IRelationInternal index in cascadedRelations)
            {
                var entities = referringEntities[index];

                if (entities.Count == 0)
                {
                    continue;
                }

                index.CascadedDelete(entities, this);

                // At this point these entities should have been removed from the database
                // In order to avoid foreign key validation, clear the collection
                //
                // TODO: It might be better to do foreign key validation from the
                // other direction: check if anything refers storedEntities
                entities.Clear();
            }

            // Validate the entities that are referring to the deleted entities
            helper.ValidateForeignKeys(relations.Referring, referringEntities);
        }