Entity Framework Core 2.1 comes with a nifty little feature: an In-Memory Database setting. What this means, is that with a single option setting, your tests can interact directly with the database (or at least EF's impression of the database) but not actually touch any physical database. In other words, you can write unit tests for data access; an example: // Arrange DbContextOptions options = new DbContextOptionsBuilder() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .EnableSensitiveDataLogging() .Options; using (var context = new ApplicationDbContext(options)) { context.Database.EnsureDeleted(); ResourceCategory resourceCategory = new ResourceCategory() { Name = "TestCategory" } }; // Act _applicationDbContext.ResourceCategories.Add(resourceCategory); _applicationDbContext.SaveChanges(); // Assert Assert.Equal("TestCategory", context.ResourceCategories.First().Name); To just quickly explain what this is doing: we have a DbContext called ApplicationDbContext and we're building a set of options on top of that context.


I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.

Stay tuned to my blogtwitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.


This article is related to


devops,tutorial,unit testing,entity framework,entity framework core