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

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

Question

When attempting to construct a generic repository pattern using EntityFramwork 6, I see the following error:

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

This is the key:

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

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

DBEntities' code is as follows:

 public partial class DBEntities : DbContext
    { ... }
1
0
9/23/2016 6:42:53 PM

Accepted Answer

I believe that what you are attempting to do is what you should be accomplishing using DbContext in EF6.

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