All,
I am using Microsoft Management Objects (SMO) to create a database with it's
associated tables.
All works well until I try to define more than one index as a primary key.
See code below:
// Add two primary key indices
Index pkIndex = new Index(myTable, "PK_MyTable");
pkIndex.IndexKeyType = IndexKeyType.DriPrimaryKey;
pkIndex.IndexedColumns.Add(new IndexedColumn(pkIndex, "Column1"));
pkIndex.IndexedColumns.Add(new IndexedColumn(pkIndex2, "Column2"));
// Add index to the table
myTable.Indexes.Add(pkIndex);
// Create table in database
myTable.Create(); // Throws exception!
Also tried it this way:
// Add two primary key indices
Index pkIndex = new Index(myTable, "PK_MyTable1");
pkIndex.IndexKeyType = IndexKeyType.DriPrimaryKey;
pkIndex.IndexedColumns.Add(new IndexedColumn(pkIndex, "Column1"));
Index pkIndex2 = new Index(myTable, "PK_MyTable2");
pkIndex2.IndexKeyType = IndexKeyType.DriPrimaryKey;
pkIndex2.IndexedColumns.Add(new IndexedColumn(pkIndex2, "Column2"));
// Add index to the table
myTable.Indexes.Add(pkIndex);
myTable.Indexes.Add(pkIndex2);
// Create table in database
myTable.Create(); // Throws exception!
Does anyone know how to define more than one primary key for a table using
SMO?
Thanks in advance.All,
Disregard this question.
Problem was with a column definition, not with the primary key index creatio
n.
Note that the following code works:
// Add two primary key indices
Index pkIndex = new Index(myTable, "PK_MyTable");
pkIndex.IndexKeyType = IndexKeyType.DriPrimaryKey;
pkIndex.IndexedColumns.Add(new IndexedColumn(pkIndex, "Column1"));
pkIndex.IndexedColumns.Add(new IndexedColumn(pkIndex2, "Column2"));
// Add index to the table
myTable.Indexes.Add(pkIndex);
// Create table in database
myTable.Create();
No comments:
Post a Comment