Skip to content

Instantly share code, notes, and snippets.

@randyburden
Created September 11, 2017 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randyburden/4e8fca394718d59ab5a52aed5f5a246f to your computer and use it in GitHub Desktop.
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.
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