Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

What is and why use dbSet in repository constructor?

$
0
0

I see from many examples when making repositories in asp.net core using entityframeworkcore that it is common to write constructors something like this: 

private readonly ApplicationDbContext _context;
private readonly DbSet<Project> dbSet;

public ProjectRepository(ApplicationDbContext context)
{
     _context = context;
     dbSet = context.Set<Project>();
}

Now this is how I wrote mine. I'm not sure if I need to pass in both the general context as well as a dbSet of type T. But I use the context just in case I need to get other types than the Project table.

What is the advantages of using a dbSet, and what is it? Is it just a holder for a query, but in that case why not just use the context like normal? Is it to enforce the repository to only be able to make queries to that type? 


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>