How do i get the PK Id value during/after insert?

How do i get the PK Id value during/after insert?

Old forum URL: forums.lhotka.net/forums/t/7232.aspx


superkuton posted on Monday, July 06, 2009

How do i get the PK Id value during/after insert?


I have a Student (Parent) Object and a ContactAddress (Child) Object.

After creating and saving the Student Object, the user will then create a ContactAddress Object, passing in the StudentID (int, PK, identity field).

In the UI, I have implement the ostudent = ostudent.save() method.

But the Student Object is not updated with the StudentID assigned by the db.

I cant; follow the book example because the Project object uses GUID.

triplea replied on Monday, July 06, 2009

It really is up to you how you want to achieve this. Are you using straight ADO Connection/Commands? If so are you using stored procs or just straight SQL. In any case you manually need to get the PK value back from the database (usually an output parameter on a stored proc or a select IDENTITY_SCOPE() ) and populate the Id field of your root BO.

I only have the 3.0.5 ProjectTracker example but even if you use later versions of CSLA the principal is the same. Check the Resource class because there an int is used as the Id and the example is clear.

superkuton replied on Monday, July 06, 2009

I am using LINQ to CSLA as DAC, and I am using sprocs to insert, update and delete both the Student and the Contact Address Objects.

I am new to CSLA and I am studying the 3.5 ProjectTracker. The Resource class has the following code:

  <Transactional(TransactionalTypes.TransactionScope)> _
  Protected Overrides Sub DataPortal_Insert()

        Using ctx = ContextManager(Of ProjectTracker.DalLinq.PTrackerDataContext).GetManager(ProjectTracker.DalLinq.Database.PTracker)
            Dim newId As Integer?
            Dim newLastChanged As System.Data.Linq.Binary = Nothing
            ctx.DataContext.addResource(_lastName, _firstName, newId, newLastChanged)
            _id = CInt(newId)
            _timestamp = newLastChanged.ToArray
            FieldManager.UpdateChildren(Me)
        End Using

  End Sub


where is it getting the newId? How do i get my StudentID?
Thanks.

RockfordLhotka replied on Monday, July 06, 2009

You should read chapters 17 and 18 of Expert 2008 Business Objects, as they should help quite a lot.

The code you are looking at uses LINQ to SQL. And L2S is being used to invoke a stored procedure in the database. One of the really cool features of L2S is that it can "wrap" a stored procedure with a .NET method, and so this code is calling a .NET method that wraps a call to a stored procedure.

The stored procedure does the insert, and returns the new id and timestamp values as output parameters. Those output parameters are ByRef parameters in the .NET method call, and so they come back out through the parameters of the method call and into the variables used in the code you listed.

superkuton replied on Monday, July 06, 2009

Thanks, Rocky.

I am finally getting the new PK Id.

Thanks for CSLA.

Copyright (c) Marimer LLC