Knight Data Access Layer
Indexes
Basic Tasks C# > Mapping > Indexes

Indexes are defined differently than primary keys or composite keys.  There are two options, TableIndex which consists of one or more columns for a table or a ColumnIndex which is simply an attribute on a Column.   Indexes are automatically created when the Table does not exist and a Setup command is issued.  If the table already exists and a new TableIndex or or ColumnIndex attribute is added, it will not be added to the table.  There are methods available to deal with indexes such as CreateIndex, IndexExists, and GetIndexesForTable.

 

Here is a table index consisting of First Name and Last Name

 

[TableIndex(isUnique: false, indexName: "IX_Prospects_FirstLast", commaDelimitedListOfColumns: "FirstName,LastName")]

public class Prospect

{

public int ProspectId { get; set; }

public string FirstName { get; set; }

public string LastName { get; set; }

}

 

Here is a column index for just the column MovieName.  The default is non-unique ascending. You can specify the uniqueness, the acension, and the index name.

 

public class Movie

{

public int MovieId { get; set; }

 

[ColumnIndex(isUnique: true, isAscending: true, indexName: "IX_Movies_MovieName")]

public string MovieName { get; set; }

}