Log in

Advanced Doing your own Primary keys

From MDrivenWiki
Revision as of 08:49, 2 December 2022 by Stephanie (talk | contribs)

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 go like 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 that 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 between these that we provide:

2019-06-18 17h54 18.png

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

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

If you use another mapper with not autoassign you must choose SaveAction=Freeze - this is so that the key may not be changed after first insert.