コード例 #1
0
ファイル: Repository.cs プロジェクト: EusebioDM/DiscRepoEF
        private TEntity GetEntityFromRepo(DbContext context, TEntity localEntity)
        {
            EntityEntry entry = context.Entry(localEntity);
            EntityKeys  key   = Helper <TEntity> .GetKeys(entry);

            return(context.Find <TEntity>(key.Keys.ToArray()));
        }
コード例 #2
0
        private void RemoveEntitiesNotInUpdateRecusively(DbContext context, EntityEntry currentEntry, HashSet <EntityKeys> alreadyTraversed)
        {
            EntityKeys keys = Helper <TEntity> .GetKeys(currentEntry);

            bool haventTraversedThisEntity = !alreadyTraversed.Contains(keys);

            if (!haventTraversedThisEntity)
            {
                return;
            }

            alreadyTraversed.Add(keys);
            foreach (NavigationEntry property in currentEntry.Navigations)
            {
                property.Load();
                dynamic propertyCurrentValue = property.CurrentValue;
                if (property.Metadata.IsCollection())
                {
                    RemoveEntitiesFromCollectionThatWereNotPartOftheUpdateAndCallRecursively(context, propertyCurrentValue, alreadyTraversed);
                }
                else
                {
                    CallThisMethodRecusivelyWithChildEntity(context, property, alreadyTraversed);
                }
            }
        }
コード例 #3
0
ファイル: Helper.cs プロジェクト: EusebioDM/DiscRepoEF
        public static bool EntriesAreEqual(EntityEntry first, EntityEntry second)
        {
            EntityKeys firstKey  = GetKeys(first);
            EntityKeys secondKey = GetKeys(second);

            return(firstKey.Equals(secondKey));
        }
コード例 #4
0
        private void RemoveNoLongerPresentEntities(TEntity entity)
        {
            using (DbContext context = contextFactory.CreateDbContext(new string[0]))
            {
                EntityEntry entry = context.Entry(entity);
                EntityKeys  key   = Helper <TEntity> .GetKeys(entry);

                TEntity root = context.Find <TEntity>(key.Keys.ToArray());
                RemoveEntitiesNotInUpdateRecusively(context, context.Entry(root), new HashSet <EntityKeys>());
                context.SaveChanges();
            }
        }
コード例 #5
0
ファイル: Helper.cs プロジェクト: EusebioDM/DiscRepoEF
        public static EntityKeys GetKeys(EntityEntry entry)
        {
            EntityKeys keys = new EntityKeys();

            foreach (PropertyEntry propety in entry.Properties)
            {
                if (propety.Metadata.IsPrimaryKey())
                {
                    keys.AddKey(propety.CurrentValue);
                }
            }
            return(keys);
        }
コード例 #6
0
        private void RemoveEntitiesFromCollectionThatWereNotPartOftheUpdateAndCallRecursively(DbContext context, dynamic entitiesThatNeedToBeFiltered, HashSet <EntityKeys> alreadyTraversed)
        {
            List <dynamic> toBeDeleted = new List <dynamic>();

            foreach (dynamic entity in entitiesThatNeedToBeFiltered)
            {
                EntityEntry entry     = context.Entry(entity);
                EntityKeys  entityKey = Helper <TEntity> .GetKeys(entry);

                if (!entitiesThatShouldBeInUpdate.Contains(entityKey))
                {
                    toBeDeleted.Add(entity);
                }
                RemoveEntitiesNotInUpdateRecusively(context, entry, alreadyTraversed);
            }

            Action <dynamic> deleteFromCollectionOfEntities = e => entitiesThatNeedToBeFiltered.Remove(e);

            toBeDeleted.ForEach(deleteFromCollectionOfEntities);
        }