Created
September 11, 2017 19:33
-
-
Save randyburden/4e8fca394718d59ab5a52aed5f5a246f to your computer and use it in GitHub Desktop.
C# test class with 2 Entity Framework-related tests intended to verify that there are no pending migrations and no migrations that need to be created.
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
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using System.Data.Entity.Migrations; | |
using System.Data.Entity.Migrations.Design; | |
using System.Diagnostics; | |
using System.Linq; | |
namespace YabbaDabbaDoo | |
{ | |
[TestClass] | |
public class EntityFrameworkMigrationTests | |
{ | |
[TestMethod] | |
[TestCategory("RunOnBuild")] | |
public void VerifyThereAreNoPendingMigrations() | |
{ | |
// Arrange | |
var config = new MyDbMigrationsConfiguration(); | |
var dbMigrator = new DbMigrator(config); | |
// Act | |
var pendingMigrations = dbMigrator.GetPendingMigrations().ToList(); | |
// Visual Assertion | |
Trace.WriteLine(pendingMigrations); | |
// Assert | |
Assert.AreEqual(0, pendingMigrations.Count(), "There are pending EF migrations that need to be ran."); | |
} | |
[TestMethod] | |
[TestCategory("RunOnBuild")] | |
public void VerifyDatabaseIsCompatibleWithModel() | |
{ | |
// Arrange | |
var context = new MyDbContext(); | |
// Act | |
var isCompatible = context.Database.CompatibleWithModel(false); | |
// Visual Assertion | |
if (!isCompatible) | |
{ | |
var config = new MyDbMigrationsConfiguration(); | |
var scaffolder = new MigrationScaffolder(config); | |
var pendingMigration = scaffolder.Scaffold("MissingMigration"); | |
Trace.WriteLine("Missing Migration:"); | |
Trace.WriteLine(""); | |
Trace.WriteLine(pendingMigration.UserCode); | |
} | |
// Assert | |
Assert.IsTrue(isCompatible, "The EF model is not compatible with the database. An EF migration needs to be created. See output for sample of missing migration."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment