Return MySQL auto_increment primary ID on object insert

Return MySQL auto_increment primary ID on object insert

Postby elion » Mon 16 Feb 2015 09:29

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!
elion
 
Posts: 29
Joined: Wed 10 Dec 2014 07:15

Re: Return MySQL auto_increment primary ID on object insert

Postby elion » Mon 16 Feb 2015 10:55

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!?

elion
 
Posts: 29
Joined: Wed 10 Dec 2014 07:15

Re: Return MySQL auto_increment primary ID on object insert

Postby AlexP » Mon 16 Feb 2015 12:05

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.
AlexP
Devart Team
 
Posts: 5525
Joined: Tue 10 Aug 2010 11:35

Re: Return MySQL auto_increment primary ID on object insert

Postby elion » Mon 16 Feb 2015 13:06

It works. Thank you!
elion
 
Posts: 29
Joined: Wed 10 Dec 2014 07:15

Re: Return MySQL auto_increment primary ID on object insert

Postby AlexP » Mon 16 Feb 2015 13:19

Glad to see that the issue was resolved. If you have any further questions, feel free to contact us.
AlexP
Devart Team
 
Posts: 5525
Joined: Tue 10 Aug 2010 11:35


Return to EntityDAC