English 中文(简体)
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))
  {
      //... 
  }
最佳回答

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.

问题回答

Close and Dispose in SqlConnection are functionally equivalent. Therefore, as long as the connection is closed -- and I think you ll find that it is, but don t take my word for it -- the sample code works.

But since you asked, you should probably dispose of it anyway. It will do no harm, and will stop others from asking the same question.





相关问题
named connection not found (Entity framework problem)

I m building multi-project Application where some UserControl, the user control has a Entitymodel object (myDBContainer db = new myDBContainer()), when i drop my user control on my a form i got the ...

EF4 POCO - Updating a navigation property

I have a Recommendation object and a FeedbackLevel object and the FeedbackLevel object is a navigation property inside a Recommendation object Recommendation int EntityKey; FeedbackLevel Level; ...

How can I add constraints to an ADO.NET Entity?

I know how to mark a group of fields as primary key in ADO.NET entities but i haven t found a way to declare unique constraints or check constraints. Is this feature missing on the designer or on the ...

Using sql date data type and EF4

I have a table w/ a sql date data type. When I look at the EDM markup, the storage element reflects this. The conceptual entity has a data type of DateTime (there doesn t appear to be a Date data type)...

RIA services, EF and stored procs

I have this problem for 2 months now How can I use stored procedures whith RIA. I was using LinqToSql and everithing works fine. I ve made an class in designer and mapped it to a SP. Now in EF I ...

Mocking Entity Context in EF4

I am using VS2010 B2 and EF4 B2 and trying to use Rhino Mocks to mock the entity context generated by EEF. var context = MockRepository.GenerateMock<SomeDBEntities>(); IObjectSet<TxMode> ...

热门标签