New row not in query result

6 posts, 0 answers
  1. Darren
    Darren avatar
    10 posts
    Member since:
    Oct 2010

    Posted 13 Mar 2012 Link to this post

    In the example below the second count is the same as the first.  I can find the new object using it's key but not with a query.
    c2 should be one more than c1.

                    Product p = new Product();
                    p.ID = Guid.NewGuid();
                    p.ProductName = "test";

                    int c1 = context.Products.Count();
                    context.Add(p);
                    int c2 = context.Products.Count();

    How do I change the configuration so context changes are reflected in query results?
  2. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 14 Mar 2012 Link to this post

    Hi,

    the changes made in the context are not stored yet in the database, but the query is executed on the server, not on the client. If you need to push the content of the client (that is, the added object) to the server without actually committing the data, please use the context.FlushChanges() call before the second query is done. Or did you forget to put a context.SaveChanges() there?

    Greetings,
    Thomas
    the Telerik team
    Telerik OpenAccess ORM Q1 2012 release is here! Check out what's new or download a free trial >>
  3. DevCraft banner
  4. Darren
    Darren avatar
    10 posts
    Member since:
    Oct 2010

    Posted 14 Mar 2012 Link to this post

    I need to be able to query the uncommitted changes and not have a transaction open.  I wasn't expecting the context to create a transaction until SaveChanges was called.  I need a long lived context and keeping a transaction open long term isn't very desirable. 
  5. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 14 Mar 2012 Link to this post

    Hello,

    the context usually allows this by just requesting a server transaction during SaveChanges(). Only then the data is pushed to the server and normally the transaction terminates quickly.
    In your case though, where you want to to see the changed data as the result of a query that is to be executed on the server, the data must be pushed to the server first, and OpenAccess does this within the boundaries of a transaction. If the need is to have long running, flushed but not committed changes in the context you will need long running server side transactions with modifications.

    All the best,
    Thomas
    the Telerik team
    Telerik OpenAccess ORM Q1 2012 release is here! Check out what's new or download a free trial >>
  6. Darren
    Darren avatar
    10 posts
    Member since:
    Oct 2010

    Posted 14 Mar 2012 Link to this post

    So the context doesn't track anything other than changes and there's no way of querying objects directly from the context?  
  7. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 15 Mar 2012 Link to this post

    Hello Darren,

    the context tracks the instances and fields that got modified and thus is able to give the server the changes during SaveChanges(). It does not provide functionality to aggregate arbitrary changes on client side with server side query results. Using the FlushChanges method, you can push the changes to the server in a transaction and hold that transaction running until you call SaveChanges. At the moment, there is no out of the box solution for querying uncommitted data on the client besides using LINQ to Objects on your own. But this will not combine its results with server side query results.

    Greetings,
    Thomas
    the Telerik team
    Telerik OpenAccess ORM Q1 2012 release is here! Check out what's new or download a free trial >>
Back to Top
DevCraft banner