Studio for Entity Framework

RejectChanges for a specific Entity

  •  CinmarFred said 11 months ago:

    If I have two screens open with a C1TruedbGrids on each, one for Orders and one for Products. Each grid is attached to a ClientView using the following code.

    ProductView = (From p In _scope.GetItems(Of Product)() Select p)
    C1dgProducts.SetDataBinding(ProductView, "", True)

    OrderView = (From p In _scope.GetItems(Of Order)() Select p)
    C1dgOrders.SetDataBinding(OrderView, "", True)

    If I have pending changes on each screen and I want to reject the changes for the Orders but I do not want to reject the changes for the Product, how would I do this?

    I am assuming that the command Program.ClientCache.RejectChanges() will reject the Pending changes for all the entities.

  •  C1_MichaelE2p said 11 months ago:

    Use the client-side transactions feature of SEF. See "Client-Side Transactions
    " in "Programming Guide" in the doc, and the Transactions sample project.

  •  CinmarFred said 11 months ago:

    I am getting this error when I do the SetTransaction statement:

    "For a transaction to be used in a live view, the view must have a result selector creating new instances of a class that is public, inheritable, and has a public parameterless constructor."

    I have set it up this way:

    Private _scope As EntityClientScope
    Private viewListHeaders As C1.LiveLinq.LiveViews.View
    Private _transaction As ClientTransaction

    Private Sub CreateTransaction()

    _transaction = _scope.ClientCache.CreateTransaction
    viewListHeaders.SetTransaction(_transaction)

    End Sub

    Private Sub PopulateGrid()

    Dim s As String = "FG"

    viewListHeaders = (From p In _scope.GetItems(Of VWFMIListHeader)() _ .Where(Function(p As VWFMIListHeader) p.Requestor = s) _ .OrderByDescending(Function(p As VWFMIListHeader) p.ListKey) Select p)

    CreateTransaction()
    C1gListHeaders.SetDataBinding(viewListHeaders, "", True)

    End Sub

  •  C1_NodirT3p said 11 months ago:

    Hi

    In order to set a transaction to a view, the view must have a result selector that actually creates a new object. You can define a new class that have the fields you need or you can use anonymous class as the shortest way:

    Example:

    viewListHeaders = (From p In _scope.GetItems(Of VWFMIListHeader)()
        Where p.Requestor = s 
        Order By p.ListKey Descending
        Select New With {p.ListKey, p.Requestor}).AsDynamic() 'put other fields here
    

    Note that I "Select New" rather than "Select p". Put other fields that you need to access in the result selector.

    BTW, I used LINQ here in contrast to raw method calls. It is shorter and you don’t need to put underscores.

  •  CinmarFred said 11 months ago:

    Does the _transaction.Commit() save the changes to the database?

    When I use _transaction.Commit() it is not saving the data, if I follow it with Program.ClientCache.SaveChanges() it get saved.

    When I just use Program.ClientCache.SaveChanges() then it still save the change even though I have not commited the transaction.

    The data I am working with is from a view in SqlServer and I have a stored procedure attached to the Update Function in the mapping of this view in my .edmx file.

  •  C1_NodirT3p said 11 months ago:

    > Does the _transaction.Commit() save the changes to the database?

    No, it doesn’t. A client transaction allows canceling a certain set of client-side changes, hence client transaction.

    _transaction.Commit() only marks a change set as committed.
    _transaction.Rollback() undoes the changes.

    By setting a transaction to a view, you specify that changes made using the view are in the scope of the transaction, so when you cancel the transaction changes made by the view are rolled back. Other entities are left intact.

    When you save changes using ClientCache.SaveChanges(), all client transactions are committed automatically.

  •  CinmarFred said 11 months ago:

    Thanks, this is very helpful but now it leads to another question.

    Is there a way to save the changes for just a specific transaction to the database without saving everything in the Cache.

  •  C1_NodirT3p said 11 months ago:

    Unfortunately, no. Selectively saving changes was the main feature request in SEF since its first release. We will provide a solution in the next major version of SEF.

    The core reason is that Entity Framework does not support saving changes selectively.

    For now, as a workaround, until this feature is implemented, we suggest creating a separate ObjectContext and EntityClientCache before the changes that you want to save are made, make the changes there, save them, delete the context, refresh the saved entities in the main context. Unlike selective saving, selective refresh is supported in EF.

    Answer
  •  CinmarFred said 11 months ago:

    Thanks for your help.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.