Cardin
Cardin

Reputation: 5487

C# Reading database after SaveChangesAsync

If I do a write to a (ms)SQL database and saves the changes using SaveChangesAsync(), is there a possibility that a future read on the database could read unsaved changes?

Factors could include, whether a different DbContext, thread or process is used to access the database.

Upvotes: 0

Views: 830

Answers (2)

Aron
Aron

Reputation: 15772

Short Answer, NO (Tim P is mis-informed).

Calling DbContext.SaveChangesAsync will automagically create a Transaction for the duration of the saving.

This means that if any other thread tries to access the table, one of a number of things can occur.

Generally it means that the other Database call will block on the other thread, whilst the transaction is uncommited/not-rolledback.

Upvotes: 3

Tim P.
Tim P.

Reputation: 2942

Short answer: Yes.

It depends on several factors, such as how much you are saving to the database. If you're saving 1,000 rows/objects, and it's a slow database server, it's possible that this time window is wide enough that another thread is reading while (for example) row #879 has yet to be saved. And this has nothing to do with it being asynchronous. This is the normal concurrency problem in dealing with multi-user relational database systems.

Upvotes: 1

Related Questions