本文整理汇总了C#中DbContext.KeyMemberFor方法的典型用法代码示例。如果您正苦于以下问题:C# DbContext.KeyMemberFor方法的具体用法?C# DbContext.KeyMemberFor怎么用?C# DbContext.KeyMemberFor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DbContext
的用法示例。
在下文中一共展示了DbContext.KeyMemberFor方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Compare
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();
}