DbContext Connection events or overrides

Topics: EF Runtime, General
Oct 12, 2012 at 11:08 PM

I'd love to know when EF opens and is about to close its connection to the database. I need to write a DbContext in which opening the connection sets the app role and the app role is unset just prior to closing the connection. Unfortunately, there is no Closing event on IDbConnection to grab on to.

Oct 17, 2012 at 5:27 PM

Hi crmckenzie,

DbConnection itself is not part of the EF code and so we can't add a Closing event to it. That being said we would like to provide better event notifications to EF and this is one we would consider adding. However, it is possible for the connection to be closed outside of the control by EF meaning that even if we did add the event it would only indicate the EF is about to close the connection and not that the connection in general is about to be closed. I will try to get a work item filed to track this.


Oct 18, 2012 at 8:33 PM

I think it would be nice if EF used something like NHiberntate's ConnectionProvider. The control of the connection lifetime could then be separated from the DbContext, and users such as myself could set or unset application roles accordingly. Just an idea.

Oct 25, 2012 at 8:06 PM
Edited Oct 25, 2012 at 8:07 PM

Will that help you?

DbContext context = new DbContext();
context.Database.Connection.StateChange += Connection_StateChange;

void Connection_StateChange(object sender, StateChangeEventArgs e)
    /// Work With
    /// e.CurrentState
    /// e.OriginalState
Oct 26, 2012 at 3:37 AM

No--because I need to unset the approle before closing the connection. The StateChanged event fires after the connection has been closed--not before.

Oct 26, 2012 at 4:26 PM

Have you considered wrapping the connection and overriding Close()?

Oct 26, 2012 at 4:48 PM
Yes--that's the first thing I tried.It seems to be a rabbit-hole. You end up having to override the ConnectionFactory, Provider, Manifest, and all sorts of other stuff. The overhead to inject at the connection level is very expensive. I've been unable to to get a custom provider written and working against EF so far.

On Fri, Oct 26, 2012 at 9:26 AM, AndrewPeters <notifications@codeplex.com> wrote:

From: AndrewPeters

Have you considered wrapping the connection and overriding Close()?

Read the full discussion online.

To add a post to this discussion, reply to this email (entityframework@discussions.codeplex.com)

To start a new discussion for this project, email entityframework@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com

Excellence is an art won by training and habituation. We are what we repeatedly do. Excellence then is not an act, but a habit.

The two enemies of the people are criminals and government, so let us tie the second down with the chains of the Constitution so the second will not become the legalized version of the first.

--Thomas Jefferson