🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
Setting all model classes to use AutoInc as PrimaryKeyMapper
This page was created by Lars.olofsson on 2017-10-29. Last edited by Wikiadmin on 2026-07-29.

You can use this EAL expression in the Model Debugger to assign the AutoInc primary-key mapper to every model class that does not already have an explicit primary-key mapper; it is intended for models reversed from databases whose keys are assigned by the database.

When to use this

Use this operation after reverse engineering a database when the database columns used as primary keys are database identity/auto-increment columns. AutoInc is an MDriven-supplied primary-key mapper for normal database auto-increment behavior.

For example, a reversed Album class may have an integer AlbumId attribute that corresponds to an identity column in the database. The class needs AutoInc as its primary-key mapper so MDriven uses the database-generated key strategy rather than the default key strategy.

Do not use this bulk operation for classes whose keys are:

  • Supplied by your application.
  • Assigned by a custom mapper.
  • Composite keys.
  • Already configured with a mapper that must be retained.

For composite keys and other custom mapping cases, use Documentation:Custom OR Mapping. For a fuller explanation of primary-key strategies, see Documentation:Doing your own Primary keys.

What the expression changes

The expression creates this tagged value on each selected class:

Tagged value Value Effect
Eco.PrimaryKeyMapper AutoInc Selects the AutoInc named primary-key mapper for the class.

It only affects classes that do not already have an Eco.PrimaryKeyMapper tagged value. This is intentional: an existing mapper is not replaced.

Before you run it

  1. Confirm that the database primary-key columns are configured to generate their values, such as identity/auto-increment integer columns.
  2. Check the reversed classes and confirm that each relevant class has the correct primary-key attribute. The primary key must correspond to an attribute in the class.
  3. Identify exceptions before running the expression. For example, leave a class with a composite key or a custom application-assigned key out of the bulk change by giving it its correct mapper first.
  4. For every primary-key attribute whose value is generated by the database, set its SaveAction to DBAssigned. This tells MDriven not to provide the key value on the first save and to read the database-assigned value back after insert.

The EAL below sets only the mapper tagged value. It does not set SaveAction, create missing primary-key attributes, or correct an incorrect primary-key definition.

Run the EAL

  1. In MDriven Designer, open the model that contains the reversed classes.
  2. Open the Model Debugger. In Designer, you can right-click in applicable locations and choose Extras and then Open Model Debugger….
  3. Run the following EAL against the model:
Class.allInstances->select(c | c.TaggedValue->select(tv | tv.Tag='Eco.PrimaryKeyMapper')->isEmpty)
->collect(c |
let newtag = TaggedValue.Create in
(
    newtag.Tag := 'Eco.PrimaryKeyMapper';
    newtag.Value := 'AutoInc';
    newtag.ModelElement := c
  )
)
  1. Review the affected classes in the model. Each class that previously lacked the mapper now has the Eco.PrimaryKeyMapper tagged value with value AutoInc.
  2. Check each database-assigned primary-key attribute and set SaveAction to DBAssigned where needed.
  3. Save the model and test an insert. A newly inserted object should receive the key generated by the database and retain it for related foreign-key use.

How the expression works

The expression finds all Class instances, filters to classes where no tagged value has the tag Eco.PrimaryKeyMapper, and creates a new TaggedValue for each remaining class.

Expression part Meaning
Class.allInstances Gets the classes in the model.
select(...isEmpty) Keeps only classes with no existing Eco.PrimaryKeyMapper tagged value.
TaggedValue.Create Creates a tagged value.
newtag.Value := 'AutoInc' Sets the selected mapper name.

Important mapping considerations

A primary-key mapper is only one part of persistence mapping. In particular:

  • DBAssigned is required on a database-assigned primary-key attribute so MDriven reads the generated value after saving.
  • If your application supplies a key value, use Freeze rather than DBAssigned so the key remains unchanged after the first insert.
  • Package-level OR mapping settings control whether per-class mapping properties are visible. See Documentation:Doing your own Primary keys.
  • A package default superclass can affect key mapping. When a default superclass and a child class use incompatible key arrangements, configure the mapping approach described in Documentation:Custom OR Mapping and Documentation:Working with Code and Persistence Mapping.

See also