Identity is zero when add entity to collection

Posts   
 
    
robertpnl
User
Posts: 6
Joined: 31-Jul-2007
# Posted on: 01-Aug-2007 22:04:31   

Hi,

I'm a newbie about O/R mapping structures, but this is the situation.

I have two tables with a m:1 relation. I have defined this by the designer: table1: ID (PK), table2ID, textcolumn table2: ID (PK), textcolumn So, the relation is: table1.table2ID -> table2.ID

I created a UserControl which will used for databinding. Internally in this UserControl I create a kind of combobox with all rows of table2.

Databinding will be done by property ID of this UC from the main program.

Oke, back to the UserControl. All rows will be loaded by datasource Table2EntityCollection. I use GetMulti() to load.

With this usercontrol, user enter some text. During typing, a row match what user types will show. But, sometime will the user enter a new value.

Then, I add a new entity to the collection (method Add(NewEntity)). But after that, the Id of this new entity is zero and not a new value of autoincrement.

Also, I can use the Save() method of the entity, but the value will me add to the database immediatly what I don't.

So, why can I not get the ID when add the entity to collection? (And maybe, is this the correct way about logical ways)

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 02-Aug-2007 04:22:32   

Hi Robert,

Then, I add a new entity to the collection (method Add(NewEntity)). But after that, the Id of this new entity is zero and not a new value of autoincrement.

Also, I can use the Save() method of the entity, but the value will me add to the database immediatly what I don't.

So, why can I not get the ID when add the entity to collection? (And maybe, is this the correct way about logical ways)

If I understood, you want to add a new entity to collection, you don't want to save such entity to DB but you want the entity have the new autoincrement ID...

Autoincrement is a DB function, no LLBLGen, so if you add a new entity to collection they will have the newest ID only when you save it. There are concurrency issues in your approach.

Why you need new newest ID without save the entity? (for set the table1.table2ID FK I guess)

You have two options:

A. (recommended) when the user selects the Table2Entity use such object to set the Table1Entity FK instead of set the table2ID. Something like:

Table2Entity table2EntitySelected = (Table2Entity) myCombo.SelectedItem;

// this will sync PK-FK after save.
table1Entity.Table2Entity = table2EntitySelected;

// save recursively, so Table2Entity will be saved too.
table1Entity.Save(true);

B. Get the newest ID by yourself.

table2EntityToAddToCollection.table2ID = myMethodGetNextID();

I hope this makes sense to you.

David Elizondo | LLBLGen Support Team
robertpnl
User
Posts: 6
Joined: 31-Jul-2007
# Posted on: 02-Aug-2007 08:51:25   

hi daelmo,

Thank you for your very fast answer (in the middle of the night? ) and information.

simple_smile