How can I reset an EF7 InMemory provider between unit tests?

后端 未结 7 1217
忘掉有多难
忘掉有多难 2021-01-31 13:01

I am trying to use the EF7 InMemory provider for unit tests, but the persistent nature of the InMemory database between tests is causing me problems.

The following code d

7条回答
  •  心在旅途
    2021-01-31 13:35

    Simply change your code definition of DbContextOptionsBuilder to be like following :

            var databaseName = "DatabaseNameHere";
            var dbContextOption = new DbContextOptionsBuilder()
                                        .UseInMemoryDatabase(databaseName, new InMemoryDatabaseRoot())
                                        .Options;
    

    new InMemoryDatabaseRoot() creates a new database without the issue of Id's persisting. So you don't need now for :

           [TestCleanup]
           public void Cleanup()
           {
               _context = null;
           }
    

提交回复
热议问题