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

Primary Key Overview

Integer or long primary keys are automatically mapped.  If the default convention is used then no mapping is required.  Primary keys can be of any supported type.  It is possible to have composite primary keys.  See the Composite Keys topic.

 

Primary Key Types

 

Automatic Mapping of Primary Keys

Primary key class properties are automatically mapped to the corresponding primary key in the database.  If the provider supports Identity and the primary key property is an integer or a long this will be the default.  If the primary key property is a GUID, it will default to the GUID property type.  Otherwise it will default to Assign primary key type.  These are the valid conventions for the automatic mapping.  Set the Mapper.DefaultPrimaryKeyName to override the default convention.

Id

TableNameId (Example:  PersonId)

 

Mapping with Attributes

<ColumnMapping("Person_Id",System.Data.DbType.Int32), PrimaryKey(PrimaryKeyType.Identity)>

Public Property PersonId() As Integer

 

Mapping with Code

Dim db As IDataHelper = DataHelper.SessionFactory()

Dim table As New Table(GetType(Person))

table.TableName = "People"

Dim field As New Field(table, "PersonId", "Person_Id", True)

field.KeyType = PrimaryKeyType.Identity

table.Fields.Add(field)

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

 

Mapping with XML

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