Another 'Newbie' question - Identity inserts

Posts   
 
    
CliffH
User
Posts: 52
Joined: 03-May-2006
# Posted on: 17-May-2006 17:42:02   

Can anyone give me a quick example of how to retrieve a saved record in a table with an identity column, i.e., you default all identity columns to 0, you save a header record, but to save the related data you need to know the number of the just saved header record.

Any suggestion grateful.

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 18-May-2006 02:38:47   

CompanyId is an identity column and a FK in Employee. This is using SelfServicing

CustomerEntity customer = new CustomerEntity();
EmployeeEntity employee = new EmployeeEntity();

customer.Name = "Test Company";
employee.Customer = customer;
employee.Name = "Test Employee";

// This is a recursive save of the company that will also save the employee with the 
// proper CustomerId FK.
customer.Save(true);

// At this point now customer.CustomerId will have a value != 0 and 
// employee.CustomerId will have the same value.

CliffH
User
Posts: 52
Joined: 03-May-2006
# Posted on: 18-May-2006 08:01:22   

Thanks for this. Beginning to see the power of this product. Will also try UnitOfWork as well.