title: Unit Testing Exceptions thrown by Entity Framework Core
body: <p>I am using the in Memory database in order to unit test methods that use the Entity Framework Core.</p>
  
  <p>I set up the database context as follows:</p>
  
  <pre><code>var builder = new DbContextOptionsBuilder&lt;DatabaseContext&gt;().UseInMemoryDatabase();
  
  var context = new DatabaseContext(builder.Options);
  var databaseElements = Enumerable.Range(1, 10)
      .Select(i =&gt; new DatabaseElement
      {
          Id = i,
          Name = "Name of Element"
      });
      context.DatabaseElements.AddRange(databaseElements);
      context.SaveChanges();
      _databaseContext = context;
  ...
  </code></pre>
  
  <p>I use this database context for the tests.
  This worked fine.</p>
  
  <p>Is it possible to use this in order to throw Entity Framework exceptions?</p>
  
  <p>It would for example be great if I could test if my method handels those exceptions as excpected (e.g. there is no Connection to the database).</p>