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

asp.net-mvc c# entity-framework-6

Question

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
    { ... }
1
0
9/23/2016 6:42:53 PM

Accepted 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>();
    }
}
0
9/23/2016 6:40:33 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow