public static bool EntriesAreEqual(EntityEntry first, EntityEntry second)
        {
            EntityKeys firstKey  = GetKeys(first);
            EntityKeys secondKey = GetKeys(second);

            return(firstKey.Equals(secondKey));
        }
        private Entity GetEntityFromRepo(DbContext context, Entity localEntity)
        {
            EntityEntry entry = context.Entry(localEntity);
            EntityKeys  key   = HelperFunctions <Entity> .GetKeys(entry);

            return(context.Find <Entity>(key.Keys.ToArray()));
        }
        private void RemoveNoLongerPresentEntities(Entity entity)
        {
            using (DbContext context = contextFactory.CreateDbContext(new string[0]))
            {
                EntityEntry entry = context.Entry(entity);
                EntityKeys  key   = HelperFunctions <Entity> .GetKeys(entry);

                Entity root = context.Find <Entity>(key.Keys.ToArray());
                RemoveEntitiesNotInUpdateRecusively(context, context.Entry(root), new HashSet <EntityKeys>());
                context.SaveChanges();
            }
        }
        public static EntityKeys GetKeys(EntityEntry entry)
        {
            EntityKeys keys = new EntityKeys();

            foreach (var propety in entry.Properties)
            {
                if (propety.Metadata.IsPrimaryKey())
                {
                    keys.AddKey(propety.CurrentValue);
                }
            }
            return(keys);
        }
        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 = HelperFunctions <Entity> .GetKeys(entry);

                if (!entitiesThatShouldBeInUpdate.Contains(entityKey))
                {
                    toBeDeleted.Add(entity);
                }
                RemoveEntitiesNotInUpdateRecusively(context, entry, alreadyTraversed);
            }
            Action <dynamic> deleteFromCollectionOfEntities = e => entitiesThatNeedToBeFiltered.Remove(e);

            toBeDeleted.ForEach(deleteFromCollectionOfEntities);
        }
        private void RemoveEntitiesNotInUpdateRecusively(DbContext context, EntityEntry currentEntry, HashSet <EntityKeys> alreadyTraversed)
        {
            EntityKeys keys = HelperFunctions <Entity> .GetKeys(currentEntry);

            bool haventTraversedThisEntity = !alreadyTraversed.Contains(keys);

            if (haventTraversedThisEntity)
            {
                alreadyTraversed.Add(keys);
                foreach (var property in currentEntry.Navigations)
                {
                    property.Load();
                    dynamic propertyCurrentValue = property.CurrentValue;
                    if (property.Metadata.IsCollection())
                    {
                        RemoveEntitiesFromCollectionThatWereNotPartOftheUpdateAndCallRecursively(context, propertyCurrentValue, alreadyTraversed);
                    }
                    else
                    {
                        CallThisMethodRecusivelyWithChildEntity(context, property, alreadyTraversed);
                    }
                }
            }
        }