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 9272141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:47:54+00:00 2026-06-18T15:47:54+00:00

i using below repository pattern. when i instance from the class getting this exception:

  • 0

i using below repository pattern. when i instance from the class getting this exception:

 public interface IRepository<TEntity> : IDisposable where TEntity : class
{
    IQueryable<TEntity> GetQuery();
    IEnumerable<TEntity> GetAll();
    IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate);
    TEntity Single(Expression<Func<TEntity, bool>> predicate);
    TEntity First(Expression<Func<TEntity, bool>> predicate);
    void Add(TEntity entity);
    void Delete(TEntity entity);
    void Attach(TEntity entity);
    void SaveChanges();
    void SaveChanges(SaveOptions options);
}

public class GenericRepository<TEntity> : IRepository<TEntity> where TEntity : class
{
    private ObjectContext _context;
    private IObjectSet<TEntity> _objectSet;

    public GenericRepository(ObjectContext context)
    {
        _context = context;
        _objectSet = _context.CreateObjectSet<TEntity>();
    }

    public IQueryable<TEntity> GetQuery()
    {
        return _objectSet;
    }

    public IEnumerable<TEntity> GetAll()
    {
        return GetQuery().AsEnumerable();
    }

    public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate)
    {
        return _objectSet.Where<TEntity>(predicate);
    }

    public TEntity Single(Expression<Func<TEntity, bool>> predicate)
    {
        return _objectSet.Single<TEntity>(predicate);
    }

    public TEntity First(Expression<Func<TEntity, bool>> predicate)
    {
        return _objectSet.First<TEntity>(predicate);
    }

    public void Delete(TEntity entity)
    {
        if (entity == null)
        {
            throw new ArgumentNullException("entity");
        }

        _objectSet.DeleteObject(entity);
    }

    public void Add(TEntity entity)
    {
        if (entity == null)
        {
            throw new ArgumentNullException("entity");
        }

        _objectSet.AddObject(entity);
    }

    public void Attach(TEntity entity)
    {
        _objectSet.Attach(entity);
    }

    public void SaveChanges()
    {
        _context.SaveChanges();
    }

    public void SaveChanges(SaveOptions options)
    {
        _context.SaveChanges(options);
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing && _context != null)
        {
            _context.Dispose();
            _context = null;
        }
    }
}

using:

 var db = new AdventureWorks2012Entities();  
 IRepository<Person> person = new GenericRepository<Person>();

exception:
enter image description here

enter image description here

  • 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-18T15:47:55+00:00Added an answer on June 18, 2026 at 3:47 pm

    Problem is that you are using Entity Framework 4.1 or later, which has DbContext wrapper around ObjectContext. But your repository still usess ObjectContext. Add another constructor, which accepts DbContext:

    public GenericRepository(DbContext context)
    {
        _context = (context as IObjectContextAdapter).ObjectContext;
        _objectSet = _context.CreateObjectSet<TEntity>();
    }
    

    You can retrieve wrapped ObjectContext instance by casting DbContext to IObjectContextAdapter interface. Another option is updating your repository class to use latest DbContext, but that will require a little bit more coding, than simply adding one constructor:

    public class GenericRepository<TEntity> : IRepository<TEntity> 
       where TEntity : class
    {
        private DbContext _context; // instead of ObjectContext
        private DbSet<TEntity> _set; // instead of IObjectSet<TEntity>
    
        public GenericRepository(DbContext context)
        {
            _context = context;
            _set = _context.Set<TEntity>();
        }
    
        public IQueryable<TEntity> GetQuery()
        {
            return _set;
        }
    
        public IEnumerable<TEntity> GetAll()
        {
            return GetQuery().AsEnumerable();
        }
    
        public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate)
        {
            return _set.Where<TEntity>(predicate);
        }    
    
        // etc
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am using below third party API in my project development http://undesigned.org.za/2007/10/22/amazon-s3-php-class I have done
I am using below code to generate photo gallery from a folder. How can
So 99% of all DI examples using the Repository pattern with MVC (or Web
I am using EF 4 with repository pattern which has the generic query method
Premise: I'm using Entity Framework, Repository Pattern and MVP for an ASP.NET project. Question:
I have created below repository methods for Dapper ORM. public SqlMapper.GridReader QueryMultiple(string query, object
I am using the decorator pattern and am decorating a class with a constructor
I have two repository classes below, MoneyTransferRepository class is used by other classes in
I am using EF. and the repository Pattern. The specific question is how can
I'm trying to implement the repository pattern using entity framework code first rc 1.

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.