本文整理汇总了C#中Key.AddColumn方法的典型用法代码示例。如果您正苦于以下问题:C# Key.AddColumn方法的具体用法?C# Key.AddColumn怎么用?C# Key.AddColumn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Key
的用法示例。
在下文中一共展示了Key.AddColumn方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: The_Corresponding_Relationship_Should_Also_Have_The_Column_Removed
public void The_Corresponding_Relationship_Should_Also_Have_The_Column_Removed()
{
// Create database and relationships
Database db = RelationshipDatabaseLoader.GetDb();
Relationship relationship = db.Tables[1].Relationships[0];
Assert.That(relationship.PrimaryKey, Is.SameAs(db.Tables[0].Keys[0]));
Assert.That(relationship.ForeignKey, Is.SameAs(db.Tables[1].Keys[0]));
Assert.That(relationship.PrimaryKey.Columns[0].Name, Is.EqualTo("Column1"));
Assert.That(relationship.PrimaryKey.Columns[1].Name, Is.EqualTo("Column2"));
IKey originalKey = db.Tables[1].Keys[0];
IKey newKey = new Key(originalKey.Name, originalKey.Keytype);
newKey.Parent = new Table("Table2");
newKey.Parent.AddColumn(new Column("Column2"));
newKey.Description = "new description";
newKey.AddColumn("Column2");
KeyChangeOperation op = new KeyChangeOperation(db.Tables[1].Keys[0], newKey);
op.RunOperation();
op.RunSecondStep();
// Make sure the relationship is still the same, and has the same references.
Relationship relationship2 = db.Tables[1].Relationships[0];
Assert.That(relationship2, Is.SameAs(relationship));
Assert.That(relationship2.PrimaryKey, Is.SameAs(db.Tables[0].Keys[0]));
Assert.That(relationship2.ForeignKey, Is.SameAs(db.Tables[1].Keys[0]));
Assert.That(relationship2.PrimaryKey.Columns, Has.Count(2));
Assert.That(relationship2.ForeignKey.Columns, Has.Count(1));
Assert.That(relationship2.ForeignKey.Columns[0].Name, Is.EqualTo("Column2"));
}
示例2: CreateTable
private static ITable CreateTable(IDatabase db, string tableName)
{
var table = new Table(tableName);
table.AddColumn(new Column("Column1"));
table.AddColumn(new Column("Column2"));
table.AddColumn(new Column("Column3"));
var primaryKey = new Key("PrimaryKey", DatabaseKeyType.Primary);
table.AddKey(primaryKey);
var foreignKey = new Key("ForeignKey", DatabaseKeyType.Foreign);
table.AddKey(foreignKey);
primaryKey.AddColumn("Column1");
foreignKey.AddColumn("Column1");
db.AddTable(table);
return table;
}
示例3: GetKeys
private void GetKeys(ITable table)
{
DataRow[] keyRows = connector.Indexes.Select(string.Format("TABLE_NAME = '{0}' AND TABLE_SCHEMA = '{1}'", table.Name.Replace("'", "''"), table.Schema));
for (int i = 0; i < keyRows.Length; i++)
{
DataRow keyRow = keyRows[i];
DatabaseKeyType keyType;
string indexKeyType = keyRow["CONSTRAINT_TYPE"].ToString();
if (indexKeyType == "PRIMARY KEY")
{
keyType = DatabaseKeyType.Primary;
}
else if (indexKeyType == "UNIQUE")
{
keyType = DatabaseKeyType.Unique;
}
else
{
continue;
}
Key key = new Key
{
Name = keyRow["INDEX_NAME"].ToString(),
IsUserDefined = false,
Keytype = keyType,
Parent = table,
IsUnique = true
};
// Fill Columns
List<DataRow> keyColumnRows = new List<DataRow>();
keyColumnRows.AddRange(connector.Columns.Select(string.Format("TABLE_NAME = '{0}' AND TABLE_SCHEMA = '{1}' AND COLUMN_NAME = '{2}'", table.Name.Replace("'", "''"), table.Schema, keyRow["COLUMN_NAME"])));
while ((i < keyRows.Length - 1) && (string)keyRows[i + 1]["TABLE_NAME"] == table.Name && (string)keyRows[i + 1]["INDEX_NAME"] == (string)keyRow["INDEX_NAME"])
{
i++;
keyRow = keyRows[i];
keyColumnRows.AddRange(connector.Columns.Select(string.Format("TABLE_NAME = '{0}' AND TABLE_SCHEMA = '{1}' AND COLUMN_NAME = '{2}'", table.Name.Replace("'", "''"), table.Schema, keyRow["COLUMN_NAME"])));
}
// Fill Columns
foreach (DataRow keyColumnRow in keyColumnRows)
{
key.AddColumn((string)keyColumnRow["COLUMN_NAME"]);
}
key.ResetDefaults();
table.AddKey(key);
}
}
示例4: GetForeignKeys
private void GetForeignKeys(IDatabase db)
{
DataTable keysTable = connector.GetForeignKeyConstraints();
ITable foreignTable;
for (int i = 0; i < keysTable.Rows.Count; i++)
{
var keyRow = keysTable.Rows[i];
ITable parentTable = db.GetTable((string)keyRow["PrimaryTable"], (string)keyRow["PrimaryTableSchema"]);
foreignTable = db.GetTable((string)keyRow["ForeignTable"], (string)keyRow["ForeignTableSchema"]);
var currentKeyName = keyRow["ForeignKeyName"].ToString();
// Don't process keys of tables that the user hasn't selected
if (parentTable == null || foreignTable == null)
continue;
Key key = new Key
{
Name = currentKeyName,
IsUserDefined = false,
Keytype = DatabaseKeyType.Foreign,
Parent = parentTable
};
//////////////////
key.IsUnique = connector.GetUniqueIndexes().Select(string.Format("INDEX_NAME = '{0}'", currentKeyName)).Length > 0;
//////////////////
foreignTable.AddKey(key);
// Get Foreign Key columns
DataRow[] foreignKeyColumns = connector.GetForeignKeyColumns().Select(string.Format("CONSTRAINT_NAME = '{0}' AND TABLE_NAME = '{1}'", key.Name.Replace("'", "''"), foreignTable.Name));
foreach (DataRow row in foreignKeyColumns)
key.AddColumn((string)row["COLUMN_NAME"]);
key.ReferencedKey = parentTable.GetKey((string)keyRow["PrimaryKeyName"]);
bool isUnique = false;
if (foreignTable.FirstPrimaryKey != null)
{
isUnique = true;
foreach (var col in foreignTable.FirstPrimaryKey.Columns)
{
if (!key.Columns.Contains(col))
{
isUnique = false;
break;
}
}
}
key.IsUnique = isUnique;
}
}
示例5: GetDb
internal static Database GetDb()
{
var db = TestDatabaseLoader.TestDatabase();
Table table2 = new Table("Table2");
table2.AddColumn(new Column("Column1"));
table2.AddColumn(new Column("Column2"));
table2.AddColumn(new Column("Column3"));
Key foreignKey = new Key("FK_Table2", DatabaseKeyType.Foreign);
foreignKey.Parent = table2;
foreignKey.ReferencedKey = db.Tables[0].Keys[0];
foreignKey.AddColumn("Column1");
foreignKey.AddColumn("Column2");
table2.AddKey(foreignKey);
db.AddTable(table2);
new DatabaseProcessor().CreateRelationships(db);
return db;
}
示例6: Should_Return_This
public void Should_Return_This()
{
const string expectedXML = KeyWithColumnsXml;
Table table1 = new Table("Table1");
table1.AddColumn(new Column("Column1"));
Key key = new Key("PrimaryKey");
key.Parent = table1;
key.AddColumn("Column1");
string outputXML = key.Serialise(new DatabaseSerialisationScheme());
outputXML = XmlSqueezer.RemoveWhitespaceBetweenElements(outputXML);
Assert.That(outputXML, Is.EqualTo(expectedXML));
}
示例7: GetNewTable
private Model.Table GetNewTable(string schema, string tableName)
{
Interfaces.Events.RaiseObjectBeingProcessedEvent(tableName, "Table");
Model.Table table = new Model.Table(tableName, false);
#region Columns
DataRow[] columnRows = Columns.Select(string.Format("TABLE_SCHEMA = '{0}' AND TABLE_NAME = '{1}'", schema, tableName));
foreach (DataRow columnRow in columnRows)
{
bool isReadOnly = false;
if (!columnRow.IsNull("IsIdentity") && (int)columnRow["IsIdentity"] == 1)
{
isReadOnly = true;
}
else if (!columnRow.IsNull("IsComputed") && (int)columnRow["IsComputed"] == 1)
{
isReadOnly = true;
}
else if (Slyce.Common.Utility.StringsAreEqual((string)columnRow["DATA_TYPE"], "timestamp", false))
{
isReadOnly = true;
}
Column column = new Column(
(string)columnRow["COLUMN_NAME"],
false,
table,
(int)columnRow["ORDINAL_POSITION"],
Slyce.Common.Utility.StringsAreEqual((string)columnRow["IS_NULLABLE"], "YES", false),
(string)columnRow["DATA_TYPE"],
columnRow.IsNull("CHARACTER_MAXIMUM_LENGTH") ? 0 : Convert.ToInt32(columnRow["CHARACTER_MAXIMUM_LENGTH"]),
(int)columnRow["InPrimaryKey"] == 1,
columnRow.IsNull("IsIdentity") ? false : Convert.ToInt32(columnRow["IsIdentity"]) == 1,
columnRow.IsNull("COLUMN_DEFAULT") ? "" : (string)columnRow["COLUMN_DEFAULT"],
isReadOnly,
columnRow.IsNull("IsComputed") ? false : Convert.ToInt32(columnRow["IsComputed"]) == 1,
columnRow.IsNull("NUMERIC_PRECISION") ? 0 : Convert.ToInt32(columnRow["NUMERIC_PRECISION"]),
columnRow.IsNull("NUMERIC_SCALE") ? 0 : Convert.ToInt32(columnRow["NUMERIC_SCALE"]));
if (IsSupported(column))
{
table.AddColumn(column);
}
}
#endregion
#region Indexes
DataRow[] indexRows = Indexes.Select(string.Format("TABLE_SCHEMA = '{0}' AND TABLE_NAME = '{1}'", schema, tableName));
//foreach (DataRow indexRow in indexRows)
for (int i = 0; i < indexRows.Length; i++)
{
DataRow indexRow = indexRows[i];
string indexType;
string indexKeyType = indexRow["CONSTRAINT_TYPE"].ToString();
if (indexKeyType == "PRIMARY KEY")
{
indexType = DatabaseConstant.IndexType.PrimaryKey;
//continue;
}
else if (indexKeyType == "FOREIGN KEY")
{
indexType = DatabaseConstant.IndexType.ForeignKey;
//continue;
}
else if (indexKeyType == "UNIQUE")
{
//continue;
indexType = DatabaseConstant.IndexType.Unique;
}
else if (indexKeyType == "CHECK")
{
indexType = DatabaseConstant.IndexType.Check;
}
else if (indexKeyType == "NONE") //TODO check is NONE
{
indexType = DatabaseConstant.IndexType.None;
}
else
{
//continue;
throw new Exception("IndexType " + indexKeyType + " Not Defined");
}
List<DataRow> indexColumnRows = new List<DataRow>();// = IndexColumns.Select(string.Format("TABLE_NAME = '{0}' AND CONSTRAINT_NAME = '{1}'", tableName, indexRow["CONSTRAINT_NAME"]));
indexColumnRows.AddRange(Columns.Select(string.Format("TABLE_SCHEMA = '{0}' AND TABLE_NAME = '{1}' AND COLUMN_NAME = '{2}'", schema, tableName, indexRow["COLUMN_NAME"])));
while ((i < indexRows.Length - 1) && (string)indexRows[i + 1]["TABLE_NAME"] == tableName && (string)indexRows[i + 1]["CONSTRAINT_NAME"] == (string)indexRow["CONSTRAINT_NAME"])
{
i++;
indexRow = indexRows[i];
indexColumnRows.AddRange(Columns.Select(string.Format("TABLE_SCHEMA = '{0}' AND TABLE_NAME = '{1}' AND COLUMN_NAME = '{2}'", schema, tableName, indexRow["COLUMN_NAME"])));
}
bool isUnique = (int)indexRow["IS_UNIQUE"] == 1 ? true : false;
bool isClustered = (int)indexRow["IS_CLUSTERED"] == 1 ? true : false;
Index index = new Index(indexRow["CONSTRAINT_NAME"].ToString(), false, indexType, table, isUnique, isClustered);
// Fill Columns
//.........这里部分代码省略.........
示例8: CreateUniqueKeyFor
private static void CreateUniqueKeyFor(Key key)
{
IKey uniqueKey = new Key("Unique_" + key.Name, DatabaseKeyType.Unique);
key.Parent.AddKey(uniqueKey);
foreach (var column in key.Columns)
{
uniqueKey.AddColumn(column.Name);
}
}
示例9: PostProcessEntity
private void PostProcessEntity(MappingSet set, Entity entity)
{
if (entity.HasParent == false) return;
var childTable = entity.MappedTables().First();
var parentTable = entity.Parent.MappedTables().First();
// Create foreign key for parent relationship
// This code makes a major assumption: That the primary key of the child has the same columns
// as the primary key of the parent.
var name = GetNextKeyName("FK_" + parentTable.Name + "_" + childTable.Name, set);
var foreignKey = new Key(name, DatabaseKeyType.Foreign);
var primaryKey = parentTable.FirstPrimaryKey;
var childPrimaryKey = childTable.FirstPrimaryKey;
foreignKey.ReferencedKey = primaryKey;
childTable.AddKey(foreignKey);
foreach (var column in childPrimaryKey.Columns)
{
foreignKey.AddColumn(column.Name);
}
}
示例10: ProcessKeyNode
public IKey ProcessKeyNode(XmlNode node, ITable parent, IDatabase db)
{
IKey key = new Key();
key.Parent = parent;
ProcessScriptBase(key, node);
/*
<Keytype>Primary</Keytype>
<Columns>
<ColumnName>ColumnT11</ColumnName>
</Columns>
<ReferencedKey>
<KeyName>ForeignKey</KeyName>
<TableName>Table2</TableName>
</ReferencedKey>
*/
NodeProcessor proc = new NodeProcessor(node);
key.Keytype = proc.GetEnum<DatabaseKeyType>("Keytype");
key.IsUnique = proc.GetBool("IsUnique");
var columnNodeList = node.SelectNodes("Columns/ColumnName");
if (columnNodeList != null)
{
foreach (XmlNode columnRef in columnNodeList)
{
key.AddColumn(columnRef.InnerText);
}
}
return key;
}
示例11: CreateForeignKey
private static Key CreateForeignKey(MappingSet set, ITable primaryTable, ITable foreignTable)
{
var foreignKey = new Key(GetNextKeyName("FK_" + primaryTable.Name + "_" + foreignTable.Name, set), DatabaseKeyType.Foreign);
var primaryKey = primaryTable.FirstPrimaryKey;
foreignKey.ReferencedKey = primaryKey;
foreignTable.AddKey(foreignKey);
// Create the columns for the foreign keys
foreach (var column in primaryKey.Columns)
{
var newColumn = CloneColumn(primaryTable, foreignTable, column, "Foreign_");
foreignKey.AddColumn(newColumn.Name);
}
return foreignKey;
}
示例12: GetForeignKeys
private void GetForeignKeys(IDatabase db)
{
DataTable keysTable = connector.GetForeignKeyConstraints();
foreach (DataRow keyRow in keysTable.Rows)
{
ITable parentTable = db.GetTable((string)keyRow["PrimaryTable"], "");
ITable foreignTable = db.GetTable((string)keyRow["ForeignTable"], "");
// Don't process keys of tables that the user hasn't selected
if (parentTable == null || foreignTable == null)
continue;
Key key = new Key
{
Name = keyRow["PrimaryKeyName"].ToString(),
IsUserDefined = false,
Keytype = DatabaseKeyType.Foreign,
Parent = parentTable
};
//////////////////
key.IsUnique = connector.GetUniqueIndexes().Select(string.Format("INDEX_NAME = '{0}'", key.Name)).Length > 0;
//foreignTable.AddKey(key);
//////////////////
// Get Foreign Key columns
DataRow[] foreignKeyColumns = connector.GetForeignKeyColumns().Select(string.Format("CONSTRAINT_NAME = '{0}' AND TABLE_NAME = '{1}'", key.Name.Replace("'", "''"), foreignTable.Name));
foreach (DataRow row in foreignKeyColumns)
key.AddColumn((string)row["COLUMN_NAME"]);
DataRow[] keyReferencedColumnRows =
connector.IndexReferencedColumns.Select(string.Format("ForeignKey = '{0}'", keyRow["CONSTRAINT_NAME"]));
DataRow firstKeyReferencedColumnRow = keyReferencedColumnRows[0];
// Fill References
key.ReferencedKey = foreignTable.GetKey((string)firstKeyReferencedColumnRow["ReferencedKey"]);
parentTable.AddKey(key);
}
return;
}
示例13: GetKeys
private void GetKeys(ITable table)
{
List<DataRow> keyRows = connector.GetIndexRows(table.Schema, table.Name.Replace("'", "''"));
for (int i = 0; i < keyRows.Count; i++)
{
DataRow keyRow = keyRows[i];
DatabaseKeyType keyType;
string indexKeyType = keyRow["CONSTRAINT_TYPE"].ToString();
/*if (indexKeyType == "PRIMARY KEY")
keyType = DatabaseKeyType.Primary;
else*/
if (indexKeyType == "UNIQUE")
keyType = DatabaseKeyType.Unique;
else
continue;
Key key = new Key
{
Name = keyRow["INDEX_NAME"].ToString(),
IsUserDefined = false,
Keytype = keyType,
Parent = table,
IsUnique = true
};
// Fill Columns
List<DataRow> keyColumnRows = new List<DataRow>();
keyColumnRows.AddRange(connector.ColumnsBySchema[table.Schema][table.Name.Replace("'", "''")].Where(r => r[OrdColumn].ToString() == keyRow["COLUMN_NAME"].ToString()));
while ((i < keyRows.Count - 1) && (string)keyRows[i + 1]["TABLE_NAME"] == table.Name && (string)keyRows[i + 1]["INDEX_NAME"] == (string)keyRow["INDEX_NAME"])
{
i++;
keyRow = keyRows[i];
keyColumnRows.AddRange(connector.ColumnsBySchema[table.Schema][table.Name.Replace("'", "''")].Where(c => c[OrdColumn].ToString() == keyRow["COLUMN_NAME"].ToString()));
}
// Fill Columns
foreach (DataRow keyColumnRow in keyColumnRows)
key.AddColumn((string)keyColumnRow["COLUMN_NAME"]);
key.ResetDefaults();
table.AddKey(key);
}
// Add Primary Keys
var primaryKeyNames = connector.PrimaryKeyNames;
if (primaryKeyNames.ContainsKey(table.Schema) && primaryKeyNames[table.Schema].ContainsKey(table.Name))
{
Key key = new Key
{
Name = primaryKeyNames[table.Schema][table.Name],
IsUserDefined = false,
Keytype = DatabaseKeyType.Primary,
Parent = table,
IsUnique = true
};
// Fill Columns
foreach (var column in table.ColumnsInPrimaryKey)
key.AddColumn(column.Name);
key.ResetDefaults();
table.AddKey(key);
}
}
示例14: Save
//.........这里部分代码省略.........
foreach (var relationship in MainTable.Relationships)
{
if ((relationship.PrimaryKey == mainTableKey &&
relationship.ForeignKey == relatedTableKey) ||
(relationship.PrimaryKey == relatedTableKey &&
relationship.ForeignKey == mainTableKey))
{
MessageBox.Show(this, "This relationship already exists.", "Relationship exists", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
}
}
if (mainTableKey != null &&
relatedTableKey != null &&
(mainTableKey.Keytype == DatabaseKeyType.Primary &&
relatedTableKey.Keytype == DatabaseKeyType.Primary))
{
// A relationship can't have two primary keys, so we'll need to create a new 'foreign-key'
relatedTableKey = null;
}
RelatedTable = (Table)comboBoxForeignTable.SelectedItem;
if (mainTableKey == null)
{
mainTableKey = new Key
{
Name = string.Format("FK_{0}_{1}", MainTable.Name, RelatedTable.Name).GetNextName(MainTable.Relationships.Select(r => r.Name).ToList()),
IsUserDefined = true,
Keytype = DatabaseKeyType.Foreign,
Parent = MainTable,
IsUnique = mainColsAreUnique
};
foreach (Column column in GetMainColumns())
mainTableKey.AddColumn(column.Name);
}
if (relatedTableKey == null)
{
relatedTableKey = new Key
{
Name = string.Format("FK_{0}_{1}", RelatedTable.Name, MainTable.Name).GetNextName(RelatedTable.Relationships.Select(r => r.Name).ToList()),
IsUserDefined = true,
Keytype = DatabaseKeyType.Foreign,
Parent = RelatedTable,
IsUnique = relatedColsAreUnique
};
foreach (Column column in GetRelatedColumns())
relatedTableKey.AddColumn(column.Name);
}
mainTableKey.ReferencedKey = relatedTableKey;
relatedTableKey.ReferencedKey = mainTableKey;
if (Relationship == null)
{
Relationship newRelationship = new RelationshipImpl();
newRelationship.Database = MainTable.Database;
newRelationship.IsUserDefined = true;
newRelationship.Schema = MainTable.Schema;
if (relatedTableKey.Keytype == DatabaseKeyType.Primary)
{
newRelationship.AddThisTo(RelatedTable, MainTable);
newRelationship.Name = string.Format("FK_NH_{0}_{1}", RelatedTable, MainTable).GetNextName(RelatedTable.Relationships.Select(r => r.Name).ToList());
//newRelationship.PrimaryCardinality = mainColsAreUnique ? ArchAngel.Interfaces.Cardinality.One : ArchAngel.Interfaces.Cardinality.Many;
//newRelationship.ForeignCardinality = relatedColsAreUnique ? ArchAngel.Interfaces.Cardinality.One : ArchAngel.Interfaces.Cardinality.Many;
newRelationship.ForeignCardinality = mainColsAreUnique ? ArchAngel.Interfaces.Cardinality.One : ArchAngel.Interfaces.Cardinality.Many;
newRelationship.PrimaryCardinality = relatedColsAreUnique ? ArchAngel.Interfaces.Cardinality.One : ArchAngel.Interfaces.Cardinality.Many;
示例15: The_Original_Foreign_Key_Should_Be_Modified
public void The_Original_Foreign_Key_Should_Be_Modified()
{
var db1 = RelationshipDatabaseLoader.GetDb();
IKey originalKey = db1.Tables[1].Keys[0];
originalKey.Description = "old description";
IKey newPrimaryKey = new Key(db1.Tables[0].Keys[0].Name);
newPrimaryKey.Parent = new Table("Table1");
newPrimaryKey.Parent.AddColumn(new Column("Column1"));
newPrimaryKey.Description = "new description";
newPrimaryKey.AddColumn("Column1");
IKey newForeignKey = new Key(originalKey.Name);
newForeignKey.Parent = new Table("Table2");
newForeignKey.Parent.AddColumn(new Column("Column2"));
newForeignKey.Parent.AddColumn(new Column("Column3"));
newForeignKey.Description = "new description";
newForeignKey.AddColumn("Column3"); // This is the change.
newForeignKey.ReferencedKey = newPrimaryKey;
KeyChangeOperation op = new KeyChangeOperation(db1.Tables[1].Keys[0], newForeignKey);
op.RunOperation();
op.RunSecondStep();
IKey key = db1.Tables[1].Keys[0];
Assert.That(db1.Tables[1].Keys, Has.Count(1));
Assert.That(key, Is.SameAs(originalKey), "Key object should still be the same");
Assert.That(key.Description, Is.EqualTo("old description"), "Should not change user set properties.");
Assert.That(key.ReferencedKey, Is.SameAs(db1.Tables[0].Keys[0]));
Assert.That(key.Columns, Has.Count(1));
Assert.That(key.Columns[0].Name, Is.EqualTo("Column3"));
Assert.That(key.Columns[0], Is.Not.SameAs(newForeignKey.Columns[0]));
Assert.That(key.Columns[0], Is.SameAs(db1.Tables[1].Columns[2]), "The new key should reference existing columns");
Assert.That(key.Parent, Is.SameAs(db1.Tables[1]), "The new key's parent should be Table1 in the existing database");
}