What is the default read lock for entity framework and how can i implement the repeatable read lock.
I am thinking that perhaps there is a global settings somewhere so that a readlock is acquired of type repeatable read by default then released once savechanges kicks in.
What is the default transaction isolation level in Entity Framework when I issue “SaveChanges()”? .Says here that the default isolation level is snapshot.How can i change to repeatable read.
1条答案
按热度按时间fnx2tebb1#
Msdn tells default isolation mode is database dependent. (2023 documentation does too, and has much better code examples.)
It also supply an example of how to handle transactions, which is what you need to benefit from what you likely want with isolation mode
repeatable read
. Beware, their example has a serious flaw, as it will swallow any exception.Here is a fixed version of their example.
context
is supposed to be an instance of a class derived fromDBContext
.As warned by Panagiotis Kanavos, this kind of locking should be avoided as much as possible. It reduces scalability, increases dead locks risks, may completely blocks other users as long as the transaction is running, ...
The
try catch rollback
could be considered superfluous, as disposing an ongoingDbTransaction
causes it to rollback (and theusing
will ensures it gets disposed).