Beispiel #1
0
        private void Accept_Cliked(object sender, RoutedEventArgs e)
        {
            try
            {
                Dictionary <string, string> childToParentRelation = new Dictionary <string, string>();

                string parentTable = this.SelectedParentTable.ItemKey.ToString();

                string childTable = this.SelectedChildTable.ItemKey.ToString();

                UIElement[] parentBoxes = this.uxParentColumns.FindVisualControls(typeof(ComboBoxTool));

                foreach (ComboBoxTool childBox in this.uxChildColumns.Children)
                {
                    string parentBoxName = childBox.Name.Replace(childComboName, parentComboName);

                    ComboBoxTool parentBox = (ComboBoxTool)parentBoxes.First(b => ((ComboBoxTool)b).Name == parentBoxName);

                    if (childBox.SelectedItem == null || parentBox.SelectedItem == null)
                    {
                        continue;
                    }

                    string childColumnName = ((DataItemModel)childBox.SelectedItem).ItemKey.ToString();

                    string parentColumnName = ((DataItemModel)parentBox.SelectedItem).ItemKey.ToString();

                    if (childToParentRelation.ContainsKey(childColumnName))
                    {
                        throw new ApplicationException($"Cannot have duplicate selections for child {childBox.SelectedItem}.");
                    }

                    if (Integrity.GetGlobalColumnDataType(childColumnName) != Integrity.GetGlobalColumnDataType(parentColumnName))
                    {
                        throw new ApplicationException($"Inconsistent Data Types {parentColumnName} – {childColumnName}.");
                    }

                    childToParentRelation.Add(childColumnName, parentColumnName);
                }

                this.DatabaseRelation.ParentTable = parentTable;

                this.DatabaseRelation.ChildTable = childTable;

                this.DatabaseRelation.RelationshipName = this.uxRelationName.Content.ToString();

                this.DatabaseRelation.Columns.Clear();

                foreach (ColumnObjectModel column in Integrity.GetObjectModel(childTable))
                {
                    if (!childToParentRelation.ContainsKey(column.ColumnName))
                    {
                        continue;
                    }

                    column.ForeignConstraintName = this.DatabaseRelation.RelationshipName;

                    column.ForeignKeyColumn = childToParentRelation[column.ColumnName];

                    column.ForeignKeyTable = this.DatabaseRelation.ParentTable;

                    column.IsVertualRelation = (this.DatabaseRelation.RelationType == RelationTypesEnum.VirtualRelation);

                    column.IsForeignkey = true;

                    this.DatabaseRelation.Columns.Add(new ColumnRelationMapModel
                    {
                        ChildColumn           = column.ColumnName,
                        ChildTable            = this.DatabaseRelation.ChildTable,
                        ForeignConstraintName = this.DatabaseRelation.RelationshipName,
                        ParentColumn          = childToParentRelation[column.ColumnName],
                        ParentTable           = this.DatabaseRelation.ParentTable
                    });
                }

                this.DialogResult = true;

                //this.Close();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.GetFullExceptionMessage());
            }
        }
        private void CheckColumnIntegrity(string propertyName)
        {
            if (!Integrity.KeepColumnsUnique)// || !Integrity.HasGlobalColumn(this.Column.ColumnName))
            {
                return;
            }

            if (Integrity.IsColumnInThisTable(this.TableName, this.Column.ColumnName))
            {
                return;
            }

            if (this.Column.Precision == 0)
            {
                this.Column.Precision = Integrity.GetGlobalPrecision(this.Column.ColumnName);
            }

            if (this.Column.Scale == 0)
            {
                this.Column.Scale = Integrity.GetGlobalScale(this.Column.ColumnName);
            }

            if (this.Column.MaxLength == 0)
            {
                this.Column.MaxLength = Integrity.GetGlobalMaxLength(this.Column.ColumnName);
            }

            SqlDbType?globalDataType = Integrity.GetGlobalColumnDataType(this.Column.ColumnName);

            if (globalDataType.HasValue)
            {
                this.Column.SqlDataType = globalDataType.Value;
            }

            if (this.Column.FriendlyName.IsNullEmptyOrWhiteSpace() || propertyName == "ColumnName")
            {
                this.Column.FriendlyName = Integrity.GetFriendlyName(this.Column.ColumnName);
            }

            if (this.Column.Description.IsNullEmptyOrWhiteSpace() || propertyName == "ColumnName")
            {
                this.Column.Description = Integrity.GetDescription(this.Column.ColumnName);
            }

            if ((Integrity.AllowDatabaseRelations || Integrity.AllowVertualRelations) &&
                Integrity.IsPrimaryKeyColumn(this.Column.ColumnName))
            {
                //this.Column.IsForeignkey = true;

                //if (!Integrity.AllowDatabaseRelations && Integrity.AllowVertualRelations)
                //{
                //  this.Column.IsVertualRelation = Integrity.AllowVertualRelations;
                //}

                //this.Column.ForeignKeyTable = Integrity.GetPrimaryKeyTable(this.Column.ColumnName);

                //this.Column.ForeignConstraintName = Integrity.BuildForeighKeyName(this.Column.ForeignKeyTable, this.TableName);

                //this.Column.ForeignKeyColumn = this.Column.ColumnName;
            }
            else
            {
                this.Column.IsForeignkey = false;

                this.Column.ForeignKeyTable = string.Empty;

                this.Column.ForeignConstraintName = string.Empty;

                this.Column.ForeignKeyColumn = string.Empty;
            }
        }