By BoLOBOOLNE payday loans

The underlying provider failed on Open

Posted: March 1st, 2010 | Filed under: .NET, Errors, Programming
Author:
No Comments →

I’ve recently been working on moving to Entity Framework for data access. It’s a lot of code to swap over so there’s a little dual mode access going on right now.

However, I learned on very simple thing: Don’t use transactions AND an EF context at the same time.

For example:
Simple User Get with EF

public class UserGetter {
    public User Get(int userId) {
        using(var context = new EfContext()) {
            return context.User.First(o => o.UserId == userId);
        }
    }
}

Other class that uses transactions AND needs a user:

public class UserAccessSetter {
    public bool SetAccess(int userId, Access access) {
        using(var ts = new TransactionScope()) {
            var user = new UserGetter().Get(userId);
            // other code that does stuff...

            ts.Complete();
        }
    }
}

Don’t do this. It will break.
Read the rest of this entry »

Tags: , , , ,