Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8022893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:24:05+00:00 2026-06-04T22:24:05+00:00

Albahari writes in c# 4.0 in a nutshell: > Although DataContext/ObjectContext implement IDisposable, you

  • 0

Albahari writes in “c# 4.0 in a nutshell”:

>
Although DataContext/ObjectContext implement IDisposable, you can (in general)
get away without disposing instances. Disposing forces the context’s connection
to dispose—but this is usually unnecessary because L2S and EF close connections
automatically whenever you finish retrieving results from a query
<<

This feels wrong and FxCop also complains if you are not diposing something that is IDisposable.

I have the following repository code:

    public abstract class Repository<TEntity> : IRepository<TEntity> where TEntity : class
    { ...
        public void Add(TEntity entity)
    {
        using (var dbContext = this.UnityContainer.Resolve<DbContext>())
        {
            dbContext.Set<TEntity>().Add(entity);
            dbContext.SaveChanges();
        }
    }

    ...

    public virtual IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> expression)
    {
       using (var dbContext = this.UnityContainer.Resolve<DbContext>())
       {
           return dbContext.Set<TEntity>().Where(expression).ToList().AsEnumerable();
       }
    }
    ...

Note: I do not return IQueryable – lazy loading should not play a role.
Resolve DbContext is configured as PerResolveLifetimeManager.

Is this approach OK or do I need to reconsider this based on Albaharis description?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:24:06+00:00Added an answer on June 4, 2026 at 10:24 pm

    You should always call dispose if class exposes it. The statement claims that EF and L2S close connection whenever they finish operation – as I know the statement is correct but in the same time ADO.NET team also closes connection in Dispose method so perhaps there are situations when connection is not closed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading C# 4.0 in a Nutshell by the Albahari brothers and I came
I am currently reading Albahari's C# 3.0 in a Nutshell book and on page
Im using the PredicateBuilder as seen here http://www.albahari.com/nutshell/predicatebuilder.aspx , everything works great, and now
I'm using LinqKit PredicateBuilder (http://www.albahari.com/nutshell/predicatebuilder.aspx) for a method that do searching. This is how
I am using the Albaharis PredicateBuilder as found here http://www.albahari.com/nutshell/predicatebuilder.aspx to filter results in
Can LinqPad query from a loose mdf file? How would it happen? (Joe Albahari,
While reading C# 3.0 in a Nutshell by Joseph and Ben Albahari , I
I am currently reading Albahari 's C# 3.0 in a Nutshell and on pg.
I'm trying to use the PredicateBuilder, as described here - http://www.albahari.com/nutshell/predicatebuilder.aspx The following code
I was reading C# 4.0 in a Nutshell by Joseph and Ben Albahari and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.