/// <summary> /// Retrieve cached entities that were previously loaded. /// </summary> public static IEnumerable <EntityEntry <TEntity> > Local <TEntity>(this DbSet <TEntity> set, params object[] keyValues) where TEntity : class { var context = set.GetDbContext(); var entries = context.ChangeTracker.Entries <TEntity>(); if (keyValues.Any() == true) { var entityType = context.FindEntityType <TEntity>(); var keys = entityType.GetKeys(); var i = 0; foreach (var property in keys.SelectMany(x => x.Properties)) { var keyValue = keyValues[i]; entries = entries.Where(e => keyValue.Equals(e.Property(property.Name).CurrentValue)); i++; } } return(entries); }
public static IEntityType FindEntityType <TEntity>(this DbSet <TEntity> set) where TEntity : class => set.GetDbContext().FindEntityType <TEntity>();