title: Unit testing EF Core using in-memory database with an eager-loaded function
body: <p>I am writing unit tests for my my Web API and cannot get the test to pass except by  removing the include (eager-loading from the method). I am using the in-memory database to provide the <code>dbcontext</code> and can't figure out why it is returning no data. Thanks in advance for any help or constructive criticism</p>
  
  <p>This is the method I am trying to test.<br>
  <strong>Note</strong>: it passes the test if I comment out the <code>.include</code> statements.</p>
  
  <pre><code>    public async Task&lt;LibraryAsset&gt; GetAsset(int assetId)
      {
          var asset = await _context.LibraryAssets
              .Include(p =&gt; p.Photo)
              .Include(p =&gt; p.Category)
              .Include(a =&gt; a.AssetType)
              .Include(s =&gt; s.Status)
              .Include(s =&gt; s.Author)
              .FirstOrDefaultAsync(x =&gt; x.Id == assetId);
  
          return asset;
      }
  </code></pre>
  
  <p>This is the base <code>DbContext</code> using the inMemory DB:</p>
  
  <pre><code>    public DataContext GetDbContext()
      {
          var builder = new DbContextOptionsBuilder&lt;DataContext&gt;();
  
          if (useSqlite)
          {
              // Use Sqlite DB.
              builder.UseSqlite("DataSource=:memory:", x =&gt; { });
          }
          else
          {
              // Use In-Memory DB.
              builder.UseInMemoryDatabase(Guid.NewGuid().ToString());
          }
  
          var DataContext = new DataContext(builder.Options);
  
          if (useSqlite)
          {
              // SQLite needs to open connection to the DB.
              // Not required for in-memory-database and MS SQL.
              DataContext.Database.OpenConnection();
          }
  
          DataContext.Database.EnsureCreated();
  
          return DataContext;
      }
  </code></pre>
  
  <p>This is the test:</p>
  
  <pre><code>    [Fact]
      public async void GetAssetById_ExistingAsset_ReturnAsset()
      {
          using (var context = GetDbContext())
          {
              ILogger&lt;LibraryAssetService&gt; logger = new 
              NullLogger&lt;LibraryAssetService&gt;();
  
              var service = new LibraryAssetService(context, _logger);
  
              var asset = new LibraryAsset
              {
                  Id = 40,
                  NumberOfCopies = 20,
                  Title = "",
                  Year = 1992,
                  Status = new Status { Id = 1 },
                  AssetType = new AssetType { Id = 1 },
                  Author = new Author { Id = 1 },
                  Category = new Category { Id = 2 },
                  Photo = new AssetPhoto { Id = 1 }
              };
  
              context.LibraryAssets.Attach(asset);
  
              context.Add(asset);
              context.SaveChanges();
  
              var actual = await service.GetAsset(40);
              Assert.Equal(40, actual.Id);
          }
      }
  </code></pre>
  
  <p>This is my first time writing unit tests and I am basically learning as I go. Please feel free to point out any other mistakes that you may have noticed as well.</p>