Force value on PK with autoincrement

Posts   
 
    
OSSistemes
User
Posts: 23
Joined: 20-Nov-2019
# Posted on: 24-Oct-2024 21:58:25   

Hi Team,

I have some problem trying to sync to databases. I have entity with GUID as PK (no problem here) but this parent entity have subentity with PK Autoincrement, when I merge origin with destiny, some times ID between databases are no the same and I always do DELETE / INSERT instead of UPDATE, because the ID between subentity are not the same, and it's no possible to sync with ID, are different!!!

Please If possible to force PK (AUTOINCREMENT) when flag isNew on subentity?

Thanks

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39760
Joined: 17-Aug-2003
# Posted on: 25-Oct-2024 18:47:03   

If I understand you correctly, you want to to insert rows from database A into database B and the table in database B has an autoincrement/identity PK so inserting the value from A wont' work, because you can't insert the value from A? (as it will autogenerated by B).

In SQL Server you can set a setting to ON https://learn.microsoft.com/en-us/sql/t-sql/statements/set-identity-insert-transact-sql?view=sql-server-ver16 which will then allow you to insert the values. However if your A and B databases go out of sync, it might be better to simply see B as a copy of A so B follows A, with a PK that's no autoincrement/identity? This might require a different entity model to work with database B however, as you can't switch identity inserts off in our framework at runtime.

Frans Bouma | Lead developer LLBLGen Pro
OSSistemes
User
Posts: 23
Joined: 20-Nov-2019
# Posted on: 28-Oct-2024 17:43:07   

If I understand you correctly, you want to to insert rows from database A into database B and the table in database B has an autoincrement/identity PK so inserting the value from A wont' work, because you can't insert the value from A? (as it will autogenerated by B).

Well, I can insert record form A to B, but if in A have ID 24 on B the ID will be 81, because ID is autoincrement and put next one value.

I will put 24 on B, and not use autoincrement, this is for mirroring, no one on database B is going to change nothing. Database A is the principal and B is only mirroring

We use mysql database

I was looking for this method

https://www.llblgen.com/Documentation/5.11/ReferenceManuals/LLBLGenProRTF/html/856EFEEF.htm

it's possible?

Walaa avatar
Walaa
Support Team
Posts: 14986
Joined: 21-Aug-2005
# Posted on: 29-Oct-2024 00:13:21   

If database B is just a mirror and will not be used otherwise, why don't you drop off the Autoincrement in database B.

And if you are using the same entity model, you need to use an alternate entity like as SubEntityB or SubEntityMirror, mapped to the same table

OSSistemes
User
Posts: 23
Joined: 20-Nov-2019
# Posted on: 06-Nov-2024 15:37:14   

A few days trying a solution but not possible, because we use same entity model, creating subentity we have the same problem, when we mapped fields with database we have same problem, the only way is using different database but this is not possible.

if not possible to ignore "is Identity" property? or by code change de value to false?

Thanks team

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39760
Joined: 17-Aug-2003
# Posted on: 07-Nov-2024 09:13:49   

It's not possible to switch this on/off at runtime, because the data is static/readonly.

However there's a solution, IF you're using Adapter : copy the llblgenproj file and load that in the designer and make sure the project in the designer gets a different name. In that project remove the sequence assignment so the field (or fields if you have multiple entities with this) isn't an identity field anymore. If you have many many fields to do this on, consider a piece of C# code in the element search panel, which offers an interface over the entire object model from C#.

Once you've done this, generate Adapter code for this project, with different namespaces so you get different code, make sure you're not overwriting your original project, and throw away the entity model code as you don't need it. You now have two DbSpecific projects and one DbGeneric project (with the entities).

To load entities from A, use your original DbSpecific adapter and to save entities into B, use the new copied DbSpecific adapter. As this copied DbSpecific adapter doesn't have the field marked as identity, it should produce a normal insert query.

If you find this too much work, you can also copy the generated sourcecode for the DbSpecific project and change the entity mapping code in the PersistenceInfoProvider.cs file for the field so it's no longer marked as identity and doesn't have a sequence anymore. You now again have two DbSpecific projects, and use the copy to save to B. This isn't really recommended, as you alter generated code.

You can alternatively also use a 3rd option with a customized PersistenceInfoProvider template, namely one which never generates sequences into the mapping data. Use this customized template in a separate TemplateBindings file and create a customized code generation preset which uses your custom templatebindings file. Then generate the code twice, once with the normal preset and once with the customized preset and you again have two DbSpecific code bases, so use the customized one to save to B. Of course it's best to give it a different namespace. More info: https://www.llblgen.com/Documentation/5.11/SDK/TemplatesandTemplatebindings.htm https://www.llblgen.com/Documentation/5.11/SDK/GeneratorandTasks.htm

The last option might be a bit cumbersome at first, but as it uses the same project file, it might be the least amount of work going forward when the project e.g. has many changes in the future, as there's no copy project to keep in sync with.

Frans Bouma | Lead developer LLBLGen Pro
OSSistemes
User
Posts: 23
Joined: 20-Nov-2019
# Posted on: 13-Nov-2024 19:17:18   

it's works!!!!! uooowwww yeahh!! THANKS TEAM!!

Now I have to improve the way to obtain new DbSpecific, but now inserts put the ID

i'm really happy

Thanks again