Page 1 of 1
Return MySQL auto_increment primary ID on object insert
Posted: Mon 16 Feb 2015 09:29
by elion
Hello!
How can I force the EntityDAC to set an auto_increment ID after an EntityDAC object was saved?
I am using MySQL 5.6, the last version of EntityDAC and Code-mapped Entities.
Code: Select all
MO := TMyObject.Create();
MO.Name := 'Test';
MO.Attach(DataModule.EntityContext1);
MO.Save;
MO.Id -> 0 // but the value of this auto_increment ID in the database 28305
What am I doing wrong? Thank you for your help!
Re: Return MySQL auto_increment primary ID on object insert
Posted: Mon 16 Feb 2015 10:55
by elion
I have created an attribute-mapped entity model. Then I have tried the same code from my last post and I get another error. It is interesting that despite of the error, a new record in the database is created, but I still get a ZERO in the Primary ID field.
Can you help me? Thx!
Code: Select all
// model
[Table('cardb.cars')]
[Model('DataModel')]
[Key('FIdCar')]
TCar = class(TMappedEntity)
private
[Column('idCar', [], asOnInsert)]
FIdCar: Int64;
...
end;
// code
audi := TCar.Create();
audi.Name := 'audi a3';
audi.Attach(DataModule1.EntityContext1);
audi.Save; -> ERROR
ERROR: [FireDAC][Phys]-308. Cannot open / define command, which does not return result sets. Try Execute/ExecSQL
audi.IdCar -> 0 Why!?
Re: Return MySQL auto_increment primary ID on object insert
Posted: Mon 16 Feb 2015 12:05
by AlexP
Hello,
You should set the following properties for аuto_increment fields in the EntityDeveloper model:
Code: Select all
Auto-Sync = OnInsert
IdGenerator = Identity
In this case, after inserting a record, the auto-incremental attribute of the Entity will be filled in.
Re: Return MySQL auto_increment primary ID on object insert
Posted: Mon 16 Feb 2015 13:06
by elion
It works. Thank you!
Re: Return MySQL auto_increment primary ID on object insert
Posted: Mon 16 Feb 2015 13:19
by AlexP
Glad to see that the issue was resolved. If you have any further questions, feel free to contact us.