Created
December 26, 2017 19:45
-
-
Save samalone/da33a527357b5e8ef51db9591a6f7cd7 to your computer and use it in GitHub Desktop.
Drop, create, and seed an EntityFramework database for unit testing
This file contains 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
Imports System.Data.Entity | |
Imports System.Data.Entity.Migrations | |
Public Class DropCreateAndSeed(Of TContext As DbContext, TMigrationConfiguration As {DbMigrationsConfiguration(Of TContext), New}) | |
Implements IDatabaseInitializer(Of TContext) | |
Public Sub InitializeDatabase(context As TContext) Implements IDatabaseInitializer(Of TContext).InitializeDatabase | |
Dim step1 = New DropCreateDatabaseAlways(Of TContext)() | |
step1.InitializeDatabase(context) | |
Dim step2 = New MigrateDatabaseToLatestVersion(Of TContext, TMigrationConfiguration)() | |
step2.InitializeDatabase(context) | |
End Sub | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When writing unit tests for an EntityFramework application, it's helpful to reset the database to a known state at the start of the unit tests. Entity framework has a
DropCreateDatabaseAlways
class that resets the database, but it doesn't call theMigration.Configuration.Seed()
function. It also has aMigrateDatabaseToLatestVersion
class that calls theSeed
function, but it doesn't reset the database. This class does both.