Efcore: Where is IDbSet<TEntity>?

Created on 12 Dec 2014  路  8Comments  路  Source: dotnet/efcore

Previous versions of DbSet implemented the interface IDbSet (see http://msdn.microsoft.com/en-us/library/gg696460(v=vs.113).aspx). This simplified testing of DbContext derived types. Is there a different pattern with EF7 or will this be added back?

Most helpful comment

@pgorbas Since DbSet<> is now an abstract base class, you can just use that to mock. You can either derive from it and implement the methods you care about, or use a mocking framework, which will do the same thing, but behind the scenes.

All 8 comments

Any thoughts on this? I looked in the code and saw that DbSet is an abstract class with pure virtual methods; however, this is still not as good as an interface when it comes to mocking for testing purposes.

@twsouthwick - Do you have specific scenarios where the interface is beneficial over the base type? The issue with interfaces is we either exclude new members or break folks who implement the interface. The base class is also slightly better for folks writing their own test doubles (rather than using a mocking framework) since you only need to implement the methods you actually use.

Closing as we have been using the base type approach in EF6.x for a few releases now and have not heard any feedback on real scenarios where this doesn't work as well.

Feel free to reopen if you want to discuss further :smile:

I am looking fo IDbSet as well . I need to mock a db context and since db contexts are basicly a group of getters and setters on DbSets, I need to be able to mock the DbSets as well.

Trying to do this under .net Core 1.0

There isn't even a public constructor.

No idea how to mock now.

@pgorbas Since DbSet<> is now an abstract base class, you can just use that to mock. You can either derive from it and implement the methods you care about, or use a mocking framework, which will do the same thing, but behind the scenes.

I inherited a project that had IDbSet\ Users... and was able to query the AspNetUsers table (not sure why this worked actually). But with this I could, for example, sort all users by UserName. This ability was kind of nice... not sure how to reproduce this behavior in Core.

@BrianARice Should be able to use the DbSet class instead.

I liked IDbSet, but I don't really need it anymore. I started coaching the use of query handlers. Makes mocking super easy.

Was this page helpful?
0 / 5 - 0 ratings