Luis Teijon Luis Teijon - 2 months ago 30
C# Question

Does not contain a definition for 'CreateObjectSet' and no extension method 'CreateObjectSet'

I'm using EntityFramwork 6 and I'm getting this error when trying to implement a generic repository pattern:


DBEntities does not contain a definition for 'CreateObjectSet' and no
extension method 'CreateObjectSet'.


Here is the code:

private DBEntities entities = null;
IObjectSet<T> _objectSet;

public GenericRepository(DBEntities _entities)
{
entities = _entities;
_objectSet = entities.CreateObjectSet<T>();
}


Here is DBEntities' code:

public partial class DBEntities : DbContext
{ ... }

Answer

I think this is what you are trying to do, this is the correct EF6 way of doing it with DbContext.

public class GenericRepository<T> where T : class
{
    private DbContext _context = null;
    private DbSet<T> _entities = null;

    public GenericRepository(DbContext context)
    {
        _context = context;
        _entities = context.Set<T>();
    }
}
Comments