Primary Keys - HomeroThompson/firehawk GitHub Wiki

Primary Keys

Unfortunately, NHibernate does not allow to set the name of primary keys. Instead it delegates to the target database the task of assign a name for each primary key constraint. Sometimes this can be inconvenient, especially when you want to establish a convention for the primary key names as you can do with the foreign key names.

Even when you can't set the name of a primary key through NHibernate mapping there are some workarounds that allow you to do it. Firehawk.MsSql extension enables you to set the name of the primary keys based on conventions. This is accomplished level by registering an {{AuxiliaryDatabaseObject}} that renames the default primary key names with the desired ones.

Next is the list of available conventions used when generating primary key names:

Default

The default behavior. No primary key name is generated, instead the database is in charge of assigning a name to the primary keys.

.ConfigureNamingConventions()
  .UseConventionForPrimaryKeyNames(PrimaryKeyNamingConvention.Default)
.EndConfig()

Example: Let's suppose that the {{Customer}} entity has an identifier property named ID which is mapped to the database as a primary key column.

public class Customer
{
  public virtual long ID { get; set; }
  public virtual string Name { get; set; }
}

Result: On the Customer table, the database will generate a primary key whose name is like:

PK_Customer_5421544781
PK_TableName_ColumnName

The primary key name consists on the PK prefix appended to the table name and the primary key property name.

.ConfigureNamingConventions()
  .UseConventionForPrimaryKeyNames(PrimaryKeyNamingConvention.PK_TableName_ColumnName)
.EndConfig()

Result: On the Customer table, Firehawk will generate a primary key whose name is like:

PK_Customer_ID
PK_TableName

The primary key name consists on the PK prefix appended to the table name.

.ConfigureNamingConventions()
  .UseConventionForPrimaryKeyNames(PrimaryKeyNamingConvention.PK_TableName)
.EndConfig()

Result: On the Customer table, Firehawk will generate a primary key whose name is like:

PK_Customer
TableName_ColumnName_PK

The primary key name consists on the table name appended to the primary key property name and the PK suffix.

.ConfigureNamingConventions()
  .UseConventionForPrimaryKeyNames(PrimaryKeyNamingConvention.TableName_ColumnName_PK)
.EndConfig()

Result: On the Customer table, Firehawk will generate a primary key whose name is like:

Customer_ID_PK
TableName_PK

The primary key name consists on the table name appended the PK suffix.

.ConfigureNamingConventions()
  .UseConventionForPrimaryKeyNames(PrimaryKeyNamingConvention.TableName_PK)
.EndConfig()

Result: On the Customer table, Firehawk will generate a primary key whose name is like:

Customer_PK
Custom

The primary key name is generated using a user defined function. The input parameters are the entity type and primary key member info.

.ConfigureNamingConventions()
  .UseCustomConventionForPrimaryKeyNames((t, c) => "mykey")
.EndConfig()