How can I prevent Entity Framework from locking the database?

entity-framework transactions

Question

Possible Duplicate:
Entity Framework with NOLOCK

I'm loading some XML from a file into a database using EF4 and.Net 4.

My class, which encapsulates the ObjectContext, provides methods for adding the marshalled objects from the XML file to the different EntityCollections that correspond to my tables.

The wrapper class constructs the ObjectContext on creation and maintains the reference in a local private class variable which is subsequently utilized by the methods. Each XML file typically includes roughly 200,000 objects.

When I'm done creating the entities, I make the following calls:

entities.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);

This generates a transaction that adheres to EntityFramework design and is created on the server. However, even on tables that are not being added to, this transaction is totally locking down my database.

To get around this, I've tried a number of different things, such as enclosing save modifications in a TransactionScope that looks like this:

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Suppress,
       new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted }))
       {
            entities.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);

            transaction.Complete();
       }

In an effort to affect the underlying transaction being utilized during SaveChanges, I have also attempted establishing the TransactionScope before creating the ObjectContext.

I would want to load many files simultaneously, however if the database is locked during save modifications, this won't be allowed.

Does anybody have a solution to this problem? Is it possible to prevent the EntityFramework from using a transaction?

Thanks in advance for any assistance.

James

1
13
5/23/2017 12:23:31 PM

Accepted Answer

I'll offer my solution in order to put this to rest.

I was essentially approaching this issue from the incorrect side of the solution; while calling, a transaction is utilized.SaveChanges() Nonetheless, you may still access the database by using the following technique:

private static FrameEntities GetEntities()
    {
        FrameEntities entities = new FrameEntities();
        entities.ExecuteStoreCommand("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;");
        return entities;
    }

This establishes the amount of isolation before you attempt to read.

Since you may read "dirty" of incomplete data, this is typically not advised, but for what I'm doing, it's acceptable.

13
5/13/2010 1:25:00 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow