I am using EF in memory database to do some unit testing. I have figured out how to populate the database with some test data and to query results through the context. The problem is that I have two db contexts. One used for adding data to the table and another with views used to access data. I don't know how to create the two contexts so that they use the same database. My unit test is returning failed, because it expected 1 result but found 0.
Any suggestions?
[Fact]
public void WhenCallingA_ThenCallReturnThis()
{
var tableOptions = new DbContextOptionsBuilder<TableContext>()
.UseInMemoryDatabase(databaseName: "MyDatabase")
.Options;
var viewOptions = new DbContextOptionsBuilder<ViewContext>()
.UseInMemoryDatabase(databaseName: "MyDatabase")
.Options;
using (var tableContext = new TableContext(tableOptions))
{
tableContext.MyTable.Add(GenerateRecord());
tableContext.SaveChanges();
}
using (var viewContext = new ViewContext(viewOptions))
{
Assert.Equal("HelloWorld", viewContext.MyTopics.Find(12).name);
}
}