Inserts and returned ids

Feb 18, 2010 at 6:55 PM

Hi,

I looked through the code and one thing wasn't clear for me.

Insert methods in data context doesn't return id of inserted object.

What if for instance I would like to insert Address object and then assign id of address to a person object?

I know that it isn't a problem to change the interface to return int value, however, if transactions are taken under consideration, the situation becomes more complex.

 

Any thoughts on this?

Thanks

Krzysztof

 

Coordinator
Feb 19, 2010 at 7:19 AM

Hello Krzysztof,

NSK "thinks in objects", and within the objectspace there are no IDs, just graphs. So the scenario you talk about is addressed composing an object graph starting from the root, and then having the DAL saving it to the database. Just to figure out a base solution, please consider this snippet:

Customer c = new Customer();

AddressInfo a = new AddressInfo();

c.Address = a;

dal.Save(c);

in such a scenario, it is up to the DAL to persist the address first, and then getting the ID back in order to be able to set the foreign key.

Feb 20, 2010 at 9:41 AM

Hi Andy,

Thanks for the information.

But what if you want to track if a customer was saved properly?

Does it mean that the only option is to use exceptions?

 

Thanks

Krzysztof