internal static IList <DbContextCheckEntry> ToDbContextCheckEntry(this IEnumerable <Difference> differences, Object objectId, string objectName, Object objectValue, string idColumnName = null) { IList <DbContextCheckEntry> entries = new List <DbContextCheckEntry>(); foreach (var item in differences) { //The property starts with a dot in KellermannDifferences var propName = item.PropertyName; if (item.PropertyName.StartsWith(".")) { propName = item.PropertyName.Remove(0, 1); } var entry = DbContextCheckEntry .ForObject(objectId.ToString(), objectName, objectValue, idColumnName) .WithProperty(propName) .Was(item.Object2Value) .InsteadOf(item.Object1Value); entries.Add(entry); } return(entries); }
public static async Task <DbContextCheckResult> Compare(DbContext expectedctx, DbContext actualContext, params string[] ignoreFields) { IEnumerable <DbSet> expectedDbSets = expectedctx.DbSets(); IList <DbContextCheckEntry> collectionCountResult = await CheckCollectionCount(expectedDbSets, actualContext); if (collectionCountResult.Count > 0) { return(new DbContextCheckResult(false, collectionCountResult)); } foreach (DbSet dbSet in expectedDbSets) { var expectedList = await dbSet.ToListAsync(); var ignoredMembers = GetIgnoredMembers(dbSet.ElementType.Name, ignoreFields); foreach (Object expectedObject in expectedList) { Object actualObject = null; var idProp = expectedctx.KeyMemberFor(dbSet.ElementType); var prop = expectedObject.GetType().GetProperty(idProp, BindingFlags.Public | BindingFlags.Instance); if (prop != null) { var idValue = prop.GetValue(expectedObject); var actualDbSet = actualContext.Set(dbSet.ElementType); actualObject = actualDbSet.Find(idValue); if (actualObject == null) { string columnName = expectedctx.MappedColumnName(dbSet.ElementType.Name, prop.Name); DbContextCheckEntry entry = DbContextCheckEntry .ForObject(idValue.ToString(), dbSet.ElementType.Name, null, columnName) .WithDescription("we couldn't find an actual object with expected id : " + idValue + " - this can be caused because the id is auto generated. You could adapt the ids of the expected object"); return(new DbContextCheckResult(false, entry)); } else { //Issue #1 : we should only check for entity that have unchanged state. if (actualContext.Entry(actualObject).State == expectedctx.Entry(expectedObject).State) { ComparisonResult cr = DbContextComparer.CompareObjects(expectedObject, actualObject, ignoredMembers); if (!cr.AreEqual) { string columnName = expectedctx.MappedColumnName(dbSet.ElementType.Name, prop.Name); return(new DbContextCheckResult(false, cr.Differences.ToDbContextCheckEntry(idValue.ToString(), dbSet.ElementType.Name, actualObject, columnName))); } } else { string columnName = actualContext.MappedColumnName(dbSet.ElementType.Name, prop.Name); DbContextCheckEntry entry = DbContextCheckEntry .ForObject(idValue.ToString(), dbSet.ElementType.Name, actualObject, columnName) .WithProperty("EntityState").Was(actualContext.Entry(actualObject).State.ToString()) .InsteadOf(expectedctx.Entry(expectedObject).State.ToString()); return(new DbContextCheckResult(false, entry)); } } } } } return(new DbContextCheckResult()); }