0

I am trying to unit test a large database. My queries will use methods like FirstOrDefaultAsync(). This results in an exception of the DbSet does not implement IDbAsyncEnumerable. For the underlying issue, and a potential solution to it, see IDbAsyncEnumerable not implemented.

I am creating my test database as follows:

  var options = new DbContextOptionsBuilder<MyDb>()
       .UseInMemoryDatabase(databaseName: myName)
       .Options;
  return new MyDb(options);

But now for the problem -- the solution there involves overriding the DbSet class with a TestDbSet that implements the missing interface IDbAsyncEnumerable. So I need my Db class to return a TestDbSet for all of the entities. The number is large, and furthermore, the db class is often regenerated (database first).

One possibility I noticed is the method

 DbContextOptionsBuilder.ReplaceService

That certainly looks intriguing -- is whatever generates the DbSet objects a "service" that one can replace? If I can tell it to always return a TestDbSet instead of a DbSet, that would solve my problem. But I can't find any documentation of what the "services" are that could be replaced by this method.

William Jockusch
  • 26,513
  • 49
  • 182
  • 323
  • 1
    `DbContextOptionsBuilder`? There is no such thing in EF6. And if you are on EF Core, simply use [In-Memory Database Provider](https://learn.microsoft.com/en-us/ef/core/providers/in-memory/). – Ivan Stoev Jan 31 '18 at 19:28
  • Sorry, wrong tag. Fixed, and clarifying question above momentarily. – William Jockusch Jan 31 '18 at 19:31

2 Answers2

0

Create partial class of your dbset created by database fitst and implement interface IDbAsyncEnumerable

programtreasures
  • 4,250
  • 1
  • 10
  • 29
0

The issue was that I was using the wrong overload of FirstOrDefaultAsync(). For my particular case, the correct one was the one in Microsoft.EntityFrameworkCore.

William Jockusch
  • 26,513
  • 49
  • 182
  • 323