public void BeforeSave() { SaveMap = new Dictionary <Type, List <EntityInfo> >(); EntityInfoGroups.ForEach(eg => { var entityInfos = eg.EntityInfos.Where(ei => ContextProvider.BeforeSaveEntity(ei)).ToList(); SaveMap.Add(eg.EntityType, entityInfos); }); SaveMap = ContextProvider.BeforeSaveEntities(SaveMap); EntitiesWithAutoGeneratedKeys = SaveMap .SelectMany(eiGrp => eiGrp.Value) .Where(ei => ei.AutoGeneratedKey != null && ei.EntityState != EntityState.Detached) .ToList(); }