none
ObjectContext.FooEntities is not seeing a new FooEntity created in separate transaction RRS feed

  • Question

  • We are seeing a strange behavior with ObjectContext where in a new entity added in a new transaction scope is not visible after the completion of scope.

            using (TransactionScope scope = new TransactionScope(
                TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
            {
                this.dataContext.AddObject("EntitySetFoo", fooEntity);
                this.dataContext.SaveChanges();
                scope.Complete();
            }

    Then we try to fetch it using

    FooEntity foo2 = this.dataContext.EntitySetFoo .Include(t => t.Field1).Where(t => t.FooID == fooId).FirstOrDefault();

    foo2 is null.

    The trick is we are unable to get a standalone repro of the above code. It only reproes in our WCF based webservice method marked with WebInvoke attribute. We have a bunch of our own framework code, all of which I don't fully understand yet.

    I was wondering if anyone has clues on when this could happen. We tried

    • calling dataContext.Refresh with the collection and explicitly with
    • played with different isolation levels

    Thanks Rags This posting is provided as is and confers to no rights.

    Wednesday, October 8, 2014 3:00 AM

Answers

  • Hello Raghavendra,

    According to your description, I made a test with a WCF project which contains the Entity Framework, and also contains the webservice method marked with WebInvoke attribute:

    The interface:

    [OperationContract]
    
            [WebInvoke(UriTemplate = "AddOrder?OrderID={x}&OrderName={y}")]
    
            void AddOrder(int orderID, string orderName);
    The real method:
    public void AddOrder(int orderID, string orderName)
    
            {
    
                Order order = new Order() { OrderID = orderID, OrderName = orderName };
    
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
    
                {
    
                    using (WCFDBEntities db = new WCFDBEntities())
    
                    {
    
                        db.AddObject("Orders", order);
    
                        db.SaveChanges();
    
                        scope.Complete();
    
                    }
    
                }
    
            }
    

    However, the new added record is saved into database successfully. I am not sure if you project is similar with mine and if not, please provide some information as the Entity Framework version, database information you are using your .NET Framework version and more related code so that we could make an exact test sample.

    >> It only reproes in our WCF based webservice method marked with WebInvoke attribute.

    Do you use the SQL Server? If it is, please try to use the SLQ Server Profiler to track the event log to see if there is an inserting operation. If not, please debug the WCF code to see if this inserting code block is executed.

    Or you could try to delcare a local dbcontext variable to add the new record as my way rather than using the global one(It seems that you current project uses this way) to see if it works or not.

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 9, 2014 2:55 AM
    Moderator