Created
February 20, 2018 20:28
-
-
Save mikebridge/a1188728a28f0f53b06fed791031c89d to your computer and use it in GitHub Desktop.
Mock EF DbContext with SQLite for XUnit Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Data.Sqlite; | |
using Microsoft.EntityFrameworkCore; | |
// ... | |
public static MyDbContext InMemoryContext() | |
{ | |
// SEE: https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/sqlite | |
var connection = new SqliteConnection("Data Source=:memory:"); | |
var options = new DbContextOptionsBuilder<MyDbContext>() | |
.UseSqlite(connection) | |
.Options; | |
connection.Open(); | |
// create the schema | |
using (var context = new MyDbContext(options)) | |
{ | |
context.Database.EnsureCreated(); | |
} | |
return new MyDbContext(options); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment