Log in

Advanced Doing your own Primary keys

From MDrivenWiki

MDriven legacy is to give the name Eco_Id to primary keys. New MDrivenSystems use <Classname>Id to make the database easier to read.

You can choose the default strategy freely if you are using MDriven Framework in Visual Studio.

In the Play-button-prototyper, you can choose from legacy or MDriven:

2019-06-18 16h00 03.png
In Code, you can do this:
     if (rbPMappMDrivenServer.Checked)
       if (_ORMappingForMDrivenServer == null)
         _ORMappingForMDrivenServer = new Eco.Persistence.ORMapping.DefaultORMappingBuilder();
         _ORMappingForMDrivenServer.ChildMapRootClass = true;
         _ORMappingForMDrivenServer.DefaultSingleLinkColumnName = "<Name>ID";
         _ORMappingForMDrivenServer.IdColumnName = "<TableName>ID";
       persistenceMapperSqlServer1.RunTimeMappingProvider = _ORMappingForMDrivenServer;
       persistenceMapperSqlServer1.NewMappingProvider = _ORMappingForMDrivenServer;

To on-a-per-class-level, set your keys (commonly needed when reversing). You must go to package and choose OrMappingMode=All. After this, you will see the following per selected class:

2019-06-18 17h49 00.png

You can name the PrimaryKey anything unique - make sure you have a corresponding attribute defined in the class.

You must also choose the PrimaryKeyMapper and this is one of the named KeyMappers that are in the PersistenceMappers SqlDatabaseConfig. If you have not created your own, you can use any of the ones we provide:

2019-06-18 17h54 18.png

The one we use by default is the DefaultEcoIdMapper. This creates an integer id from a common cursor kept in the Eco_id table.

If you use a mapper that is Auto assigned id from the database, you must state SaveAction = DBAssigned - this way we know to reread it upon insert.

If you use another mapper with no auto-assign, you must choose SaveAction=Freeze - so that the key may remain unchanged after the first insert.