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

Behaviour of saving an entity

2 Answers 101 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.
Edward
Top achievements
Rank 2
Edward asked on 24 Apr 2012, 01:00 PM
Hi,

I have a context with Visitors and I'm saving a new one and then retrieve it like:

string name = "Rico " + guid;
var b = new OADemo.Customer();
b.Lastname = name;
context.Add(b);
  
var sb = context.Customer.FirstOrDefault(x => x.Lastname == achternaam);

sb will return null.  I'm adding the new one to the context and then i'm quering this context so I thought it would find this newly inserted guy. This is not the case. Do I always need to do SaveChanges first?

Thanks!




2 Answers, 1 is accepted

Sort by
0
Accepted
Viktor Zhivkov
Telerik team
answered on 25 Apr 2012, 02:32 PM
Hi Edward,

Yes, It is required to call SaveChanges() before querying newly created instances. Executing a query the way you have posted issues a database query. Since the changes are not yet committed to the database, the internal data reader does not include the new record in the results.

If you insist on skipping the SaveChanges() method call, you can use FlushChanges() which will persist the changes to the database (this includes inserted, updated and deleted records), but leaves the database transaction uncommitted. Later you have to additionally commit or rollback the transaction. This approach gives greater control over the database operations, but introduces s bit of complexity.

Here is our documentation about the Context API related to transactions and persistence. Check the SaveChanges, FlushChanges, ClearChanges and Refresh sections.

If you have any additional questions about the Context API or OpenAccess in general do not hesitate to contact us again.

Kind regards,
Viktor Zhivkov
the Telerik team
Follow @OpenAccessORM Twitter channel to get first the latest updates on new releases, tips and tricks and sneak peeks at our product labs! 
0
Edward
Top achievements
Rank 2
answered on 25 Apr 2012, 03:46 PM
pretty neat
Tags
Data Access Free Edition
Asked by
Edward
Top achievements
Rank 2
Answers by
Viktor Zhivkov
Telerik team
Edward
Top achievements
Rank 2
Share this question
or