Refetch puzzle + Collection saving & Duplicate PKs

Posts   
 
    
Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 16-Sep-2007 14:56:30   

Hi, VB, LLBLGen 2.0, Adapter, SQL Server 2K

I have 2 questions related to collections maintained within grids. These collections are saved via appropriate Manager Classes eg:

    Sub SaveAllRegion(ByVal allRows As EntityCollection(Of RegionEntity))
        Dim adapter As New DataAccessAdapter()
        Try
            adapter.SaveEntityCollection(allRows, True, False)
        Finally
            adapter.Dispose()
        End Try
    End Sub
  1. First I'm puzzelled that I have to explicitly do a refetch (_adapter.FetchEntityCollection(allRegion, Nothing_) in order that I can see the auto-incremented PK of a newly added row.

What am I missing?

  1. When column 1 (the PK) is not an Identity field and validation prevents the entry of duplicate PKs being entered, what strategy or strategies are developers using to cope with the possibility of duplicate PKs when saving the collection to the db. i.e. When I save my collection what should I be doing to respond to an error if a duplicate PK is encountered during adapter.SaveEntityCollection(allRows, True, False)?

Hope that makes sense simple_smile Thanks

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 16-Sep-2007 22:04:06   
  1. First I'm puzzelled that I have to explicitly do a refetch (adapter.FetchEntityCollection(allRegion, Nothing) in order that I can see the auto-incremented PK of a newly added row.

What am I missing?

Where are you checking for that? (inside your method or outside it?) I mean, you are passing ByVal and you aren't returning the collecion.

  1. When column 1 (the PK) is not an Identity field and validation prevents the entry of duplicate PKs being entered, what strategy or strategies are developers using to cope with the possibility of duplicate PKs when saving the collection to the db. i.e. When I save my collection what should I be doing to respond to an error if a duplicate PK is encountered during adapter.SaveEntityCollection(allRows, True, False)?

You can catch an SD.LLBLGen.Pro.ORMSupportClasses.ORMQueryExecutionException. This is a safe way to check that because anything can happen outside your application .

David Elizondo | LLBLGen Support Team
Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 18-Sep-2007 18:25:29   
  1. The refetch problem is solved, but for the benefit of any other XtraGrid users: I checked (outside the method) and the collection WAS being refetched. Once I refreshed the grid, the incremented Identity PK was visible. BTW, passing ByRef or ByVal made no difference.

You can catch an SD.LLBLGen.Pro.ORMSupportClasses.ORMQueryExecutionException. This is a safe way to check that because anything can happen outside your application .

Am I correct in presuming that because use of the DataAdapterClass starts a transaction, then in the event of an error when saving, updates of the entire collection are rolled back?

Thanks simple_smile

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 19-Sep-2007 14:01:13   

Am I correct in presuming that because use of the DataAdapterClass starts a transaction, then in the event of an error when saving, updates of the entire collection are rolled back?

Yes, the following are copied from the docs:

you can also use the SaveEntityCollection() method of the DataAccessAdapter object which walks all objects inside the collection and, if the object is 'dirty', (which means, it's been changed and should be updated in the persistent storage) it is saved. This is all done in a transaction if no transaction is currently available.