Primary keys in DWKIT

A carefully chosen primary key guarantees that DWKit will work correctly with data. Specifying the primary key in the Data Model is mandatory if you want DWKit to work with this model properly - i.e. to display this model's records in grids with the ability to open the editing form.

If you want to display records from the View in the grid and open them in the form, then add a unique column to the grid and specify it as a primary key.

DWKit supports the following types of primary keys:

  • Primary keys of the GUID type generated on the server
  • Any database primary keys, for example identity, serial, etc.
  • Primary keys of any type generated on the application server given that you have writen a primary key generator

DWKit does not support composite primary keys.

GUID primary keys generated on the app server

In order for this mechanism to work you need to:

  • Have a primary key column of the MSSql type - uniqueidetifier, Postgres - uuid, Oracle - RAW(16) in the database.
  • Specify a Primary key property that equals to the name of this column in the data model.
  • The data model attributes should have an attribute with the name of the primary key column and it should have a Guid type.

After that, primary keys will be generated automatically without your involvement.

Primary keys generated by the database

In order for this mechanism to work you need to:

  • Have a column of the generated primary key in the table in the database, for example MSSql - identity, Postgres - serial, Oracle - GENERATED AS IDENTITY
  • Specify property Primary key that equals to the name of this column in the data model
  • The data model attributes should have an attribute with the name of the primary key column and this attribute should have a type that corresponds to the type of the primary key in the database. For example, Int16, Int32, Int64 for identity.
  • The primary key attribute in the data model should have the Calculate = true property.

After having saved the records in the database the primary key value received from the database will be updated.

Primary keys of any type generated on the application server

You can write and connect your own primary key generator to DWKit. To do that you need to:

  • Inherit the OptimaJet.DWKit.Core.ORM.PrimaryKeyGenerator class
  • Redefine its object Generate(EntityModel model, DynamicEntity entity) method and write your own primary key generation logic
public class CustomPrimaryKeyGenerator : PrimaryKeyGenerator
{
    public override object Generate(EntityModel model, DynamicEntity entity)
    {
        //custom generator logic
        return base.Generate(model, entity);
    }
} 

The EntityModel model - the description of the model and the DynamicEntity entity record which the primary key is generated for - is passed to the Generate function. This function should return the value of the primary key.

  • connect the generator to the provider; usually you can do that in the Configurator.AutoDetectProvider method, but you will need to change this method a bit. Passing the generator to the provider is done via the constructor:
var provider = new SQLServerProvider(new CustomPrimaryKeyGenerator());
  • The database table should have a primary key column of the required type
  • The data model should have a specified Primary key property equal to the name of this column
  • The data model attributes should have an attribute with the name of the primary key column and this attribute should have the type that you return from the Generate function.