Knight Data Access Layer
Composite Keys VB.NET
Basic Tasks VB.NET > Mapping > Composite Keys VB.NET

Composite Keys Overview

The highest performance is gained when using an integer or long primary key in a database.  Some legacy databases may require the use of composite keys.  Implement an IsNew property on your class so that it can be determined when an insert or update needs to occur.  See Using IsNew for more information.

 

Mapping with Attributes

<CompositeKey>

Public Property Region() As String

<CompositeKey>

Public Property Salesman() As String

 

Mapping with Code

Dim db As IDataHelper = DataHelper.SessionFactory()

Dim table As New Table(GetType(SalesPeople))

table.TableName = "SalesPeople"

Dim field As New Field(table, "Salesman", "Salesman")

field.IsCompositeKey = True

table.Fields.Add(field)

field = New Field(table, "Region", "Region")

field.IsCompositeKey = True

table.Fields.Add(field)

db.Mapper.ObjectMap.Add(GetType(Person), table)

 

Mapping with XML

<Table>
 <MappingFromDatabase>false</MappingFromDatabase>
 <Schema />
 <ClassTypeString>Models.SalesPeople, NET-Data-Access-Layer-Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</ClassTypeString>
 <ClassName>SalesPeople</ClassName>
 <TableName>SalesPeople</TableName>
 <Fields>
   <Field>
  <ColumnName>Salesman</ColumnName>
  <PropertyInfoString>Salesman</PropertyInfoString>
  <FieldTypeString>System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</FieldTypeString>
  <DatabaseType>string</DatabaseType>
  <Unique>false</Unique>
  <AllowDbNull>false</AllowDbNull>
  <IsPrimaryKey>false</IsPrimaryKey>
  <KeyType xsi:nil="true" />
  <IsReadOnly>false</IsReadOnly>
  <Length xsi:nil="true" />
  <IsCompositeKey>true</IsCompositeKey>
  <IsMaintenanceField>false</IsMaintenanceField>
   </Field>
   <Field>
  <ColumnName>Region</ColumnName>
  <PropertyInfoString>Region</PropertyInfoString>
  <FieldTypeString>System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</FieldTypeString>
  <DatabaseType>string</DatabaseType>
  <Unique>false</Unique>
  <AllowDbNull>false</AllowDbNull>
  <IsPrimaryKey>false</IsPrimaryKey>
  <KeyType xsi:nil="true" />
  <IsReadOnly>false</IsReadOnly>
  <Length xsi:nil="true" />
  <IsCompositeKey>true</IsCompositeKey>
  <IsMaintenanceField>false</IsMaintenanceField>
   </Field>  
 </Fields>
</Table>