title: Unit testing with EF Core and in memory database
body: <p>I am using ASP.NET Core 2.2, EF Core and MOQ. As you can see in the following code,  I have two tests, and running both together, with both database name "MovieListDatabase" I got an error in one of the tests with this message:</p>
  
  <pre><code>Message: System.ArgumentException : An item with the same key has already 
  been added. Key: 1
  </code></pre>
  
  <p>If I run each one separately they both pass.</p>
  
  <p>And also, having a different database name in both tests, like "MovieListDatabase1" and "MovieListDatabase2" and running both together it pass again.</p>
  
  <p>I have two questions:
  Why does this happen? and how can I refactor my code to re-use the in-memory database in both tests and make my test to look a bit cleaner?</p>
  
  <pre><code> public class MovieRepositoryTest
  {
      [Fact]
      public void GetAll_WhenCalled_ReturnsAllItems()
      {
  
          var options = new DbContextOptionsBuilder&lt;MovieDbContext&gt;()
              .UseInMemoryDatabase(databaseName: "MovieListDatabase")
              .Options;
  
          // Insert seed data into the database using one instance of the context
          using (var context = new MovieDbContext(options))
          {
              context.Movies.Add(new Movie { Id = 1, Title = "Movie 1", YearOfRelease = 2018, Genre = "Action" });
              context.Movies.Add(new Movie { Id = 2, Title = "Movie 2", YearOfRelease = 2018, Genre = "Action" });
              context.Movies.Add(new Movie { Id = 3, Title = "Movie 3", YearOfRelease = 2019, Genre = "Action" });
              context.SaveChanges();
          }
  
          // Use a clean instance of the context to run the test
          using (var context = new MovieDbContext(options))
          {
              var sut = new MovieRepository(context);
              //Act
              var movies = sut.GetAll();
  
              //Assert
              Assert.Equal(3, movies.Count());
          }
      }
  
      [Fact]
      public void Search_ValidTitlePassed_ReturnsOneMovie()
      {
          var filters = new MovieFilters { Title = "Movie 1", YearOfRelease = 2018, Genre = "Action" };
  
          var options = new DbContextOptionsBuilder&lt;MovieDbContext&gt;()
              .UseInMemoryDatabase(databaseName: "MovieListDatabase")
              .Options;
  
          // Insert seed data into the database using one instance of the context
          using (var context = new MovieDbContext(options))
          {
              context.Movies.Add(new Movie { Id = 1, Title = "Movie 1", YearOfRelease = 2018, Genre = "Action" });
              context.Movies.Add(new Movie { Id = 2, Title = "Movie 2", YearOfRelease = 2018, Genre = "Action" });
              context.Movies.Add(new Movie { Id = 3, Title = "Movie 3", YearOfRelease = 2019, Genre = "Action" });
              context.SaveChanges();
          }
  
          // Use a clean instance of the context to run the test
          using (var context = new MovieDbContext(options))
          {
              var sut = new MovieRepository(context);
  
              //Act
              //var movies = _sut.Search(_filters);
              var movies = sut.Search(filters);
  
              //Assert
              Assert.Single(movies);
          }
      }
  }
  </code></pre>
  
  <p>And this is the repository class</p>
  
  <pre><code> public class MovieRepository: IMovieRepository
  {
      private readonly MovieDbContext _moviesDbContext;
      public MovieRepository(MovieDbContext moviesDbContext)
      {
          _moviesDbContext = moviesDbContext;
      }
  
      public IEnumerable&lt;Movie&gt; GetAll()
      {
          return _moviesDbContext.Movies;
      }
  
      public IEnumerable&lt;Movie&gt; Search(MovieFilters filters)
      {
          var title = filters.Title.ToLower();
          var genre = filters.Genre.ToLower();
          return _moviesDbContext.Movies.Where( p =&gt; (p.Title.Trim().ToLower().Contains(title) | string.IsNullOrWhiteSpace(p.Title))
                                                     &amp; (p.Genre.Trim().ToLower().Contains(genre) | string.IsNullOrWhiteSpace(p.Genre))
                                                     &amp; (p.YearOfRelease == filters.YearOfRelease | filters.YearOfRelease == null)
                                               );
      }
  }
  </code></pre>
  
  <p>Thanks</p>