MySql.Data.VisualStudio.DbObjects.ForeignKey.SetName C# (CSharp) Method

SetName() public method

public SetName ( string name, bool makeUnique ) : void
name string
makeUnique bool
return void
    public void SetName(string name, bool makeUnique)
    {
      string proposedName = name;
      int uniqueIndex = 0;

      if (makeUnique)
      {
        while (true)
        {
          bool found = false;
          foreach (ForeignKey k in Table.ForeignKeys)
            if (k.Name == proposedName)
            {
              found = true;
              break;
            }
          if (!found) break;
          proposedName = String.Format("{0}_{1}", name, ++uniqueIndex);
        }
      }
      Name = proposedName;
    }

Usage Example

 private void addButton_Click(object sender, EventArgs e)
 {
     ForeignKey key = new ForeignKey(tableNode.Table, null);
     if (refTable.SelectedValue != null)
     {
         key.SetName(String.Format("FK_{0}_{1}", tableNode.Table.Name,
             refTable.SelectedValue), true);
         key.ReferencedTable = refTable.SelectedValue.ToString();
     }
     foreignKeyBindingSource.Add(key);
 }