Search code examples
entity-framework-4code-first

Does EF 4 Code First's ContextBuilder Dispose its SqlConnection?


Looking at Code First in ADO.Net EF 4 CTP 3 and wondered how the SqlConnection in their walkthrough is disposed. Is that the responsibility of ContextBuilder? Is it missing from the example?

  var connection = new SqlConnection(DB_CONN);
  var builder = new ContextBuilder<BloggingModel>();
  var connection = new SqlConnection(DB_CONN);

  using (var ctx = builder.Create(connection))
  {
      //... 
  }

Solution

  • I just realized that I can add an event handler to ObjectContext.Disposing and resolve this.

    In CTP 3 at least, Connection is not disposed when the ObjectContext is disposed.

    Since I'm subclassing ObjectContext already, I implemented IDisposable in my subclass and call Connection.Dispose() from there.