Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

.net - What is the default transaction isolation level in Entity Framework when I issue “SaveChanges()”?

What is the default transaction isolation level in Entity Framework when I issue “SaveChanges()”? I can not find it anywhere. Shall it be "Serializable"?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

SaveChanges uses implementation of DbTransaction for current store provider. It means that default transaction isolation level is set to default value for the database server. In SQL Server it is READ COMMITTED. If you want to change isolation level you can use TransactionScope. You can also override SaveChanges in your derived context and wrap base.SaveChanges() to the scope directly in overriden method.

public override void SaveChanges()
{
    // Default isolation level for TransactionScope is Serializable
    using (var scope = new TransactionScope())
    {
        base.SaveChanges();
        scope.Complete();
    }
}

You can further improve this code to allow you passing isolation level to SaveChanges etc. Once you start changing isolation levels you should do it consistently. It means you should define isolation level each time you want to run a transaction because isolation level is configured per connection and connections are reused when using connection pooling.

Edit: Default transaction level in EF6 has changed to READ COMMITTED SNAPSHOT


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...