当前位置: 首页>>代码示例>>C#>>正文


C# DbContext.KeyMemberFor方法代码示例

本文整理汇总了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();
        }
开发者ID:CedricDumont,项目名称:CExtensions-Net,代码行数:75,代码来源:DbContextComparer.cs


注:本文中的DbContext.KeyMemberFor方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。