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

  • SEARCH
  • Home
  • 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 8848753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:29:13+00:00 2026-06-14T12:29:13+00:00

I have been trying to work with Entity Framework’s Code First. I wrote the

  • 0

I have been trying to work with Entity Framework’s Code First. I wrote the below line of code

DbContext _context = new  DbContext(ConfigurationManager.ConnectionStrings["con"].ConnectionString);

However on execution, the connection remains closed. Is there something wrong with this code??

I have created a generic repository class using the DBContext shown below

public class GenericRepository<T> where T:class
  {   

public DbContext _context = new DbContext(ConfigurationManager.ConnectionStrings["con"].ConnectionString);

private DbSet<T> _dbset;

public DbSet<T> Dbset
{

    set { _dbset = _context.Set<T>(); }
    get { return _dbset; }

}

public IQueryable<T> GetAll()
{

    return Dbset;
}  
}

and I then call this class on the page load event, where Teacher is an entity class which maps to a table in the database

protected void Page_Load(object sender, EventArgs e)
{
    GenericRepository<Teacher> studentrepository = new GenericRepository<Teacher>();
    rptSchoolData.DataSource = studentrepository.GetAll().ToList();
    rptSchoolData.DataBind();
}

but the connection remains closed and there is also an InvalidOperation Exception in the ServerVersion of the context object.

Am I missing something??

  • 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-14T12:29:14+00:00Added an answer on June 14, 2026 at 12:29 pm

    This property

    public DbSet<T> Dbset
    {
        set { _dbset = _context.Set<T>(); }
        get { return _dbset; }
    }
    

    has a heavy smell to it. A setter that does nothing with value is an anti pattern big time. Do you expect to set the DbSet after creating a GenericRepository?

    I don’t understand that your code even works because you never initialize _dbset, it should throw a null object reference exception.

    The _dbset and DbSet shouldn’t be there in the first place. GetAll should return _context.Set<T>(). EF should open and close the connection all by itself. Maybe the fact that you don’t initialize the DbSet causes a connection never to open, causing problems in other pieces of code not revealed here.

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

Sidebar

Related Questions

I'm using Entity Framework 4.1 Code First and have been trying to add a
I've inherited a project which uses Entity Framework Database First. I'm trying to work
I have been trying to work with the Projector and Ray classes in order
I have been trying to make it work with either fwrite or fputcsv but
I have been trying to get nServiceBus to work with Ninject 2.0 as the
I have been trying to get this to work correctly and I think I
I have been trying to get this jsfiddle to work. So far without luck.
I have been trying to get Twitter Bootstrap btn-group with dropdown to work for
This is crazy, I have been trying for hours to get this to work.
Okay I been trying to work this out but unable too. I have a

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.