Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Interfaces With DbSet, Possible with EF Core?

Back in EF 4 it was not possible. Is it possible to use interfaces with a DbSet in EF6 or EF Core?

public class LfSp3Ctx : DbContext, ILfSp3Ctx
{
    public DbSet<ILfSp3Project> ProjectSE { get; set; }
}
like image 742
Abhijeet Avatar asked Jan 02 '18 12:01

Abhijeet


People also ask

Is DbSet part of DbContext?

The DbSet class represents an entity set that can be used for create, read, update, and delete operations. The context class (derived from DbContext ) must include the DbSet type properties for the entities which map to database tables and views. Adds the given entity to the context with the Added state.

What is the difference between DbSet and DbContext?

Intuitively, a DbContext corresponds to your database (or a collection of tables and views in your database) whereas a DbSet corresponds to a table or view in your database.

What is DbSet in Entity Framework Core?

In Entity Framework Core, the DbSet represents the set of entities. In a database, a group of similar entities is called an Entity Set. The DbSet enables the user to perform various operations like add, remove, update, etc. on the entity set.

What is the use of DbSet in Entity Framework?

A DbSet represents the collection of all entities in the context, or that can be queried from the database, of a given type. DbSet objects are created from a DbContext using the DbContext. Set method.


1 Answers

It is possible, but it requires some trickery. EF will only instantiate DbSet types directly. Thus, you'd have to create your own interface that wraps/exposes all the DbSet methods you want and the actual implementation takes the DbSet instance in its constructor. Then on the DbContext class, you have to do something like this:

IDbSet<DigimonEntity> DigimonRepository => new DbSetRepository<DigimonEntity>(this.Digimons);

DbSet<DigimonEntity> Digimons { get; set; }

public class DbSetRepository<T> : IDbSet<T> where T : class
{
    private readonly DbSet<T> _set;
    public DbSetRepository(DbSet<T> set) => _set = set;

    public Type ElementType => ((IQueryable<T>)_set).ElementType;
    public Expression Expression => ((IQueryable<T>)_set).Expression;
    public IQueryProvider Provider => ((IQueryable<T>)_set).Provider;
    public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>)_set).GetEnumerator();
    IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable<T>)_set).GetEnumerator();

    void IDataStoreRepositoryWrite<T>.Add(T entity) => _set.Add(entity);
    void IDataStoreRepositoryWrite<T>.Update(T entity) => _set.Update(entity);
    void IDataStoreRepositoryWrite<T>.Delete(T entity) => _set.Remove(entity);

    Task IDataStoreRepositoryWrite<T>.AddAsync(T entity, CancellationToken token) => _set.AddAsync(entity, token);
}

Something like this. Mine is kind of specific to the implementation I was trying to pull off at the time. :)

like image 100
Daniel Lorenz Avatar answered Oct 10 '22 22:10

Daniel Lorenz