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())); }
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); } } }
public static bool EntriesAreEqual(EntityEntry first, EntityEntry second) { EntityKeys firstKey = GetKeys(first); EntityKeys secondKey = GetKeys(second); return(firstKey.Equals(secondKey)); }
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(); } }
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); }
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); }