コード例 #1
0
        public void DeleteSelf()
        {
            // Delete all mapped references
            //List<ITable> associationTables = this.GetMappingSet().GetAssociationTablesFor(this.Database).ToList();

            foreach (ITable associationTable in this.GetMappingSet().GetAssociationTablesFor(this.Database))
            {
                foreach (Relationship rel in associationTable.Relationships.Where(r => r.PrimaryTable == this.PrimaryTable || r.PrimaryTable == this.ForeignTable))
                {
                    foreach (var reference in associationTable.MappedReferences())
                    {
                        List <ITable> sourceEntityMappedTables = ((EntityLayer.EntityImpl)reference.SourceEntity).MappedTables().ToList();
                        List <ITable> targetEntityMappedTables = ((EntityLayer.EntityImpl)reference.TargetEntity).MappedTables().ToList();

                        if (sourceEntityMappedTables.Contains(this.ForeignTable) ||
                            sourceEntityMappedTables.Contains(this.PrimaryTable) ||
                            targetEntityMappedTables.Contains(this.ForeignTable) ||
                            targetEntityMappedTables.Contains(this.PrimaryTable))
                        {
                            reference.DeleteSelf();
                        }
                    }
                }
            }

            //List<EntityLayer.Reference> mappedReferences = this.MappedReferences().ToList();

            foreach (var reference in this.MappedReferences())
            {
                reference.DeleteSelf();
            }

            var mappingSet = this.GetMappingSet();

            if (mappingSet != null)
            {
                mappingSet.RemoveMappingsContaining(this);
            }

            if (PrimaryTable != null)
            {
                PrimaryTable.RemoveRelationship(this);
            }

            if (ForeignTable != null)
            {
                ForeignTable.RemoveRelationship(this);
            }

            Database.RemoveRelationship(this);
        }