This is a migrated thread and some comments may be shown as answers.

Sample Transaction

1 Answer 92 Views
Data Access Free Edition
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Neagoe
Top achievements
Rank 1
Neagoe asked on 22 Mar 2011, 12:48 AM
We did not find any demo application "WinForms" to find your site to run as a transaction:
Simple matter:

 // Create a new category
   
Category newCategory = new Category()
   {
       CategoryName =
"MyCategory",
       Description =
"Description"
   }
;
   dbContext.Add( newCategory );
   
// Update an existing category

---------------------------------------------------------------------------------------------------
If you want to edit or delete category just added to the transaction is not closed but we have no reference ?
is updated on a category below CategoryID = 1 but in this context the value of this property is not generated upon where you reference?
Same question and delete ...
-----------------------------------------------------------------------------------------------------


   
Category updateCategory = dbContext.Categories.Where( c => c.CategoryID == 1 ).First();
   updateCategory.Description =
"NewDescription";

   
// Delete an existing category

   
Category categoryToDelete = dbContext.Categories.Where( c => c.CategoryID == 2 ).First();
   dbContext.Delete( categoryToDelete );

   
// Save Changes
   
dbContext.SaveChanges();

1 Answer, 1 is accepted

Sort by
0
Petko_I
Telerik team
answered on 24 Mar 2011, 09:21 PM
Hello Neagoe,

As far as we understand you would like to obtain access to a database calculated value for a field before the transaction has been committed. In your scenario the CategoryID value is calculated by the database server. It is recommended to use the reference for the Category to update it before it is committed as the backend calculated value cannot be retrieved without a call to the database. ORM tools do not afford to predict values generated by the database servers as the rules for the calculated values for the columns can vary greatly from one database application to another. We would therefore suggest you either commit the addition of the category so that you can later update the entity without guessing what the id might be or keep the reference to the added category in a local variable.
There is a scope operation which you can invoke to retrieve a not committed value but we would strongly advise you to use it sparingly as there are performance considerations you will need to take into account. You can flush the active transaction from the context by using our scope API. To expose the internal scope used by the OpenAccessContext you can create a separate file in which you continue the definition of the OpenAccessContext derived type in the following manner:
public partial class EntitiesModel : OpenAccessContext
    {
        public void Flush()
        {
            IObjectScope scope = this.GetScope();
            if (scope == null)
            {
                return;
            }
 
            if (scope.Transaction.IsActive)
            {
                scope.Transaction.Flush();
            }
        }
    }
We would like to point out again that this approach is not recommended and in your scenario we would try to keep the reference to the added category.

Should you have further questions, do not hesitate to contact us.

Best wishes,
Petko_I
the Telerik team
Tags
Data Access Free Edition
Asked by
Neagoe
Top achievements
Rank 1
Answers by
Petko_I
Telerik team
Share this question
or