示例#1
0
        public void AddAssociation(string associationName, string parent, string child,
            Dictionary<string, string> mappings)
        {
            Association association = new Association();
            association.Name = associationName;
            association.Parent = parent;
            association.Child = child;

            foreach (var mapping in mappings)
            {
                association.ColumnMappings[mapping.Key] = mapping.Value;
            }

            Associations.Add(association);
        }
        private void saveButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(relNameTextBox.Text.ToString()))
            {
                MessageBox.Show(Resources.ResourceManager.GetString("EmptyRelation"));
                return;
            }

            try
            {
                var association = new Association();
                association.Name = relNameTextBox.Text;
                association.Parent = parentComboBox.SelectedItem.ToString();
                association.Child = childComboBox.SelectedItem.ToString();

                for (int i = 0; i < relDataGridView.Rows.Count - 1; i++)
                {
                    association.ColumnMappings[relDataGridView.Rows[i].Cells[0].Value.ToString()] = relDataGridView.Rows[i].Cells[1].Value.ToString();
                }

                database.Associations.Add(association);

                // Add non-unique index for the association.
                Table parentTable = database.GetTable(association.Parent);
                Index index = new Index();
                index.Name = association.Name;
                index.Unique = false;

                foreach (var parentColumn in association.ColumnMappings.Keys)
                {
                    index.IndexMembers.Add(parentColumn);
                }

                parentTable.Indexes.Add(index);
            }
            catch (NullReferenceException exception)
            {
                MessageBox.Show("Complete all cells!\n" + exception.Message);
            }

            DialogResult = DialogResult.OK;
        }