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

New row not in query result

5 Answers 63 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Darren
Top achievements
Rank 1
Darren asked on 14 Mar 2012, 12:43 AM
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?

5 Answers, 1 is accepted

Sort by
0
Thomas
Telerik team
answered on 14 Mar 2012, 02:23 PM
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 >>
0
Darren
Top achievements
Rank 1
answered on 14 Mar 2012, 04:26 PM
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. 
0
Thomas
Telerik team
answered on 14 Mar 2012, 05:55 PM
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 >>
0
Darren
Top achievements
Rank 1
answered on 14 Mar 2012, 06:57 PM
So the context doesn't track anything other than changes and there's no way of querying objects directly from the context?  
0
Thomas
Telerik team
answered on 15 Mar 2012, 05:05 PM
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 >>
Tags
LINQ (LINQ specific questions)
Asked by
Darren
Top achievements
Rank 1
Answers by
Thomas
Telerik team
Darren
Top achievements
Rank 1
Share this question
or