New entities and queries

I've created a test project. A simple table named test in a database with a field name and an autoincremented Id.
When I create a new entity Test, I'm not able to find this entity in my query results.

Ex :
Test newEntity = new Test();
newEntity.Name = "newTest";
db.AddObject(newEntity);

db.SaveChanges();

Test oldEntity = newEntity;

oldEntity.Name = "oldTest";

newEntity = new Test();
newEntity.Name = "newTest2";
db.AddObject(newEntity);

Query<Test> testQuery = db.GetQuery<Test>("SELECT VALUE t FROM Test AS t");
foreach (Test entity in testQuery)
{
System.Diagnostics.Debug.WriteLine("entity name: " + entity.Name);
}

The output is : entity name: oldTest
Where is newTest2 ?

[816 byte] By [ThierryBouquain] at [2008-2-7]
# 1

You need a db.SaveChanges() to persist your changes to the database.

SteveEichert at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,ADO.NET Orcas...
# 2

Steve's response is correct, but this question touches on a couple of interesting, subtle behaviors of the framework: delegation of queries to the database and identity resolution. If you are interested in more details, check out my blog posting on this topic.

- Danny

DanielSimmons-MSFT at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,ADO.NET Orcas...
# 3
Thanks for the answer. I've seen your blog about my problem. I found this behaviour is a litte bit weird... I can get uncommited datas but not all uncommited datas.
ThierryBouquain at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,ADO.NET Orcas...
# 4

That's right. I know this seems a little bit strange, but it's a consequence of the fact that we have not built a full client-side query processor. Yes, we do quite a bit of query manipulation including esql parsing, view expansion (in order to perform mapping), etc. but we don't actually evaluate the queries on the client--that's always done on the server.

You can set the MergeOption on your query to NoCaching and get only committed data, but there's currently no way to perform an entirely client-side query or to do a full merge of server & client results. We have talked about some ideas for future releases (clearly not in the first release of the entity framework) which would give more options here including the idea of creating a truly entity-enabled version of the dataset and then expanding the LINQ over dataset work which could enable some of these scenarios.

- Danny

DanielSimmons-MSFT at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,ADO.NET Orcas...
# 5

An unrelated discussion we were having today made me think of this thread, so I wanted to do a quick follow-up...

There is one way we didn't talk about to query uncommitted changes: Using System.Transactions. The support in this CTP is not the full thing we intend it to be before we release, but you can get it to work in some controlled scenarios today. For example (copied from an email Pablo posted internally today, so hopefully he won't mind my plagarism):

using(TransactionScope tx = new TransactionScope()) {

using(ObjectContext ctx = new ObjectContext(…)) {
var q = query…

// …make some changes on the objects returned by “q”

//…save changes without committing
ctx.SaveChanges();

//…now do more queries, they’ll include the changes you flushed

//…now commit for real, or abort if don’t want the changes to be permanent
tx.Complete();
}
}

Please tread lightly in this area with this CTP, though. There are definitely some pitfalls, but this should give you some other ideas to think about this topic in general.

- Danny

DanielSimmons-MSFT at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,ADO.NET Orcas...

Visual Studio Orcas

Site Classified