Add related entites without Id's

Posts   
 
    
pokrec
User
Posts: 20
Joined: 05-Jan-2011
# Posted on: 10-Jan-2011 18:43:42   

I have ShelfEntity and BookEntity in 1:m relation and I can add new books on a shelf with use of ShelfEntity:Books ShelfEntity has public key ID, that is also foreign key for BookEntity

// I create brand new shelf, ID will be assigned by trigger in DB var shelf = new BookShelfEntity(); shelf.Name = "huge shelf"; adapter.SaveEntity(shelf ...);

^ this works perfect, now I can start adding book to the shelf

but if I will try to do it in one call:

var shelf = new BookShelfEntity() shelf.Name = "huge shelf"; shelf.Books.AddNew().Name = "some fancy book"; adapter.SaveEntity(shelf ...);

I will get exception from saving book naming that BookEntity:Shelf_ID can't be null

how can I fix this ? ( I can't just assign shelf.Id to newBook.ShelfId as Id is assigned in trigger)

BR PoKrec

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 11-Jan-2011 07:29:57   

LLBLGen doesn't know how to obtain your new Id. What database are you using? How is your trigger assigning the Id?

What LLBLGen version and runtime library version?

David Elizondo | LLBLGen Support Team
pokrec
User
Posts: 20
Joined: 05-Jan-2011
# Posted on: 11-Jan-2011 08:47:28   

For this project I use version LLBGen Pro 2.6, assemblie are also 2.6.0.0 db is Firebird

the trigger code:

SET TERM ^ ; CREATE TRIGGER SET_BS_ID FOR BOOK_SHELF ACTIVE BEFORE INSERT POSITION 0 AS BEGIN IF (NEW.ID IS NULL OR NEW.ID < 1) THEN NEW.ID = GEN_ID(GEN_BS, 1); END^ SET TERM ; ^

is there some pattern / recommendation for such case? for instance I could use stored procedure and obtain new value from generator but its the last option that I would like to use

BR PoKrec

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 11-Jan-2011 09:20:12   

For this project I use version LLBGen Pro 2.6, assemblie are also 2.6.0.0

Please refer to the Forum guideline thread to know how to pick the RTL build number. We'll also need the Firebird DQE build number.

Have you checked: Trigger based sequence values (Oracle/Firebird)

pokrec
User
Posts: 20
Joined: 05-Jan-2011
# Posted on: 11-Jan-2011 10:04:24   
  1. LLBLGen Pro version + buildnr: "LLBLGen Pro. Version: 2.6 Final (October 9th, 2009)"

  2. SD.LLBLGen.Pro.ORMSupportClasses.NET20 Runtime version: v2.0.50727 Version: 2.6.0.0

  3. FirebirdSql.Data.FirebirdClient Version: 2.1.0.0

Walaa wrote:

Have you checked: Trigger based sequence values (Oracle/Firebird)

this is not the case when i create a book shelf it does not have ID, (ID is 0 as it is default value for int) new book added to bookshelf will have reference to bookshelf entity with id 0 ... what I would like to see is when save is performed first new bookshelf is added and new id is assigned, then new book is added and book will have correct foreign key

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 11-Jan-2011 10:34:44   
  1. SD.LLBLGen.Pro.ORMSupportClasses.NET20 Runtime version: v2.0.50727 Version: 2.6.0.0

Please right click on the dll file and pick the file version.

  1. FirebirdSql.Data.FirebirdClient Version: 2.1.0.0

Same as above, but do it to the SD.LLBLGen.Pro.DQE.Firebird.NET20.dll.

when i create a book shelf it does not have ID, (ID is 0 as it is default value for int) new book added to bookshelf will have reference to bookshelf entity with id 0 ... what I would like to see is when save is performed first new bookshelf is added and new id is assigned, then new book is added and book will have correct foreign key

Forget about the book for a moment. When you create a new BookShelf, and save it, do it get back the ID? When you inspect the entity after saving, will its PK property have the correct value?

pokrec
User
Posts: 20
Joined: 05-Jan-2011
# Posted on: 11-Jan-2011 17:45:16   

thank you for help, you have pointed my to the right direction, Id was not read back, as in the designer I did not set the sequence for this field, after I did this and removed trigger from db it started working as expected thx