PrefetchPath on SaveEntity

Posts   
 
    
NickD
User
Posts: 224
Joined: 31-Jan-2005
# Posted on: 09-Nov-2005 18:18:05   

I had the scenario today where I saved an entity and then wanted to refetch it, BUT also wanted to fetch the associated UserEntity as well. So, what if you added a "SaveEntity" method that took a PrefetchPath as well that would be used only if "refetchAfterSave" was true?

Marcus avatar
Marcus
User
Posts: 747
Joined: 23-Apr-2004
# Posted on: 09-Nov-2005 19:19:59   

NickD wrote:

I had the scenario today where I saved an entity and then wanted to refetch it, BUT also wanted to fetch the associated UserEntity as well. So, what if you added a "SaveEntity" method that took a PrefetchPath as well that would be used only if "refetchAfterSave" was true?

There's actually very little benefit in prefetching when fetching a single root entity... you still need to perform 2 queries, one for the entity and one for the related entity or related collection of entities. Hence there is no different between using a prefetch and using standard subsequent fetch.

Prefetching only really delivers value when fetching root collections as it is optimised to fetch the root collection (1 query) and all the rows for the related entities in just 1 additional query. The resultsets are then merged (client side) which is where the benefit is.

Marcus

NickD
User
Posts: 224
Joined: 31-Jan-2005
# Posted on: 09-Nov-2005 21:45:58   

True, good point. I guess the only "benefit" would be that it would save a few more lines of code for me.

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 29-Jan-2006 16:02:17   

Another related issue for PrefetchPath when using databinding.


Me.Me.BindingSource1.EndEdit()
adapter.SaveEntityCollection(Me.ecFuelPrice, True)
Dim ft As EntityClasses.FuelPriceEntity = CType(Me.Me.BindingSource1.Current, _
EntityClasses.FuelPriceEntity)
adapter.FetchEntity(ft, Me.GetFuelTypePrefetchPath())
Me.BindingSource1.ResetBindings(False)

I'm using the "Fields on Related Fields" to display descriptions from related tables. In order to retrieve the values and display them in a datagridview, I have to refetch the current entity with a IPrefetchPath2 instance.

I also would like to see a PrefetchPath argument to the SaveEntity/SaveEntityCollection methods when RetrieveAfterSave is true, Maybe in 2.0?

When the related values are fetched, I then had to call the ResetBinding method for the newly retrieved values to be displayed in the DataGridView. I just point that out because I struggled to get it to work and this was the solution I came up with. simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39615
Joined: 17-Aug-2003
# Posted on: 31-Jan-2006 11:43:39   

JimHugh wrote:

Another related issue for PrefetchPath when using databinding.


Me.Me.BindingSource1.EndEdit()
adapter.SaveEntityCollection(Me.ecFuelPrice, True)
Dim ft As EntityClasses.FuelPriceEntity = CType(Me.Me.BindingSource1.Current, _
EntityClasses.FuelPriceEntity)
adapter.FetchEntity(ft, Me.GetFuelTypePrefetchPath())
Me.BindingSource1.ResetBindings(False)

I'm using the "Fields on Related Fields" to display descriptions from related tables. In order to retrieve the values and display them in a datagridview, I have to refetch the current entity with a IPrefetchPath2 instance.

I also would like to see a PrefetchPath argument to the SaveEntity/SaveEntityCollection methods when RetrieveAfterSave is true, Maybe in 2.0?

When the related values are fetched, I then had to call the ResetBinding method for the newly retrieved values to be displayed in the DataGridView. I just point that out because I struggled to get it to work and this was the solution I came up with. simple_smile

When you specify 'refetchAfterSave' to be true for SaveEntity, every saved entity is refetched. I'm not quite sure if I understand the full context of your request: the fields in the related entity, are these changed ? Could you give a clear example so I can understand it in full and decide if it's already there and should work (i.e. a bug wink ), or that I should consider it as a feature for v2 ?

Thanks.

Frans Bouma | Lead developer LLBLGen Pro
JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 31-Jan-2006 17:21:49   

It very well could be my UI Design. simple_smile

I have a DataGridView that displays some columns from an entitycollection via a bindingsource so that the user can select which record they want to edit.

The entitycollection is popluated with fields on the FieldsOnRelatedFields Tab and uses a prefetchPath to fetch those related fields.

The user interface has comboboxes in listmode that has a list of the available related fields so that the user can change the related field when editing the record. The combo box has the DisplayMember set to the description and the ValueMember set to the foreign key.

This is using DataBound controls and all of that works fine simple_smile

The problem is that when the user saves a new record and the saved record is retrieved by the SaveEntityCollection, the related entities (which were not changed, the foreign key was set on the new entity) are not retrieved to display the related description.

As a result, I have to code the following three lines after the SaveEntityCollection.

I'm sure I am just being lazy, it just seems that once an EntityCollection is retrieved with a PrefetchPath, the PrefetchPath should be stored with the EntityCollection object and optionaly reused whenever the EntityCollection is persisted.


    Private Sub SavePrice()
        Using adapter As IDataAccessAdapter = DataBase.GetAdapter()
            Try
                Me.BSFuelPricesForDate.EndEdit()
                adapter.SaveEntityCollection(Me.ecFuelPrice, True, True)
                Dim ft As EntityClasses.FuelPriceEntity = CType(Me.BSFuelPricesForDate.Current, EntityClasses.FuelPriceEntity)
                adapter.FetchEntity(ft, Me.GetFuelTypePrefetchPath)
                Me.BSFuelPricesForDate.ResetBindings(False)
            Catch ex As SD.LLBLGen.Pro.ORMSupportClasses.ORMQueryExecutionException
                DisplayError.Show(Me, ex.Message, ex)
            End Try
        End Using
    End Sub

    Private Function GetFuelTypePrefetchPath() As IPrefetchPath2
        Dim pp As IPrefetchPath2 = New PrefetchPath2(CType(EntityType.FuelPriceEntity, Integer))
        pp.Add(EntityClasses.FuelPriceEntity.PrefetchPathFuelPriceSource)
        pp.Add(EntityClasses.FuelPriceEntity.PrefetchPathFuelType)
        pp.Add(EntityClasses.FuelPriceEntity.PrefetchPathUnitsOfMeasure)
        Return pp
    End Function


Otis avatar
Otis
LLBLGen Pro Team
Posts: 39615
Joined: 17-Aug-2003
# Posted on: 01-Feb-2006 11:59:54   

Ah I now see what you mean! I was thinking in a complete different direction of events not being fired in some cases etc... wink

I see your point, though adding the prefetch path query automatically could be a thing not everyone would want as it could degrade performance.

Frans Bouma | Lead developer LLBLGen Pro