Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.