private static void CleanChanges(ModelEntity rules)
        {
            var graph      = GraphExplorer.FromRoot(rules);
            var conditions = graph.OfType <TypeAllowedAndConditions>().SelectMany(a => a.Conditions).ToList();

            conditions.ForEach(con => graph.UnionWith(GraphExplorer.FromRoot(con)));
            GraphExplorer.CleanModifications(graph);
            GraphExplorer.SetDummyRowIds(graph);
        }
Example #2
0
        /// <param name="keySelector">Unique key to retrieve ids</param>
        /// <param name="isNewPredicate">Optional filter to query only the recently inseted entities</param>
        public static int BulkInsertQueryIds <T, K>(this IEnumerable <T> entities,
                                                    Expression <Func <T, K> > keySelector,
                                                    Expression <Func <T, bool> >?isNewPredicate = null,
                                                    SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default,
                                                    bool preSaving     = true,
                                                    bool validateFirst = true,
                                                    int?timeout        = null,
                                                    string?message     = null)
            where T : Entity
            where K : notnull
        {
            using (HeavyProfiler.Log(nameof(BulkInsertQueryIds), () => typeof(T).TypeName()))
                using (Transaction tr = new Transaction())
                {
                    var t = Schema.Current.Table(typeof(T));

                    var list = entities.ToList();

                    if (isNewPredicate == null)
                    {
                        isNewPredicate = GetFilterAutomatic <T>(t);
                    }

                    var rowNum = BulkInsertTable <T>(list, copyOptions, preSaving, validateFirst, false, timeout, message);

                    var dictionary = Database.Query <T>().Where(isNewPredicate).Select(a => KeyValuePair.Create(keySelector.Evaluate(a), a.Id)).ToDictionaryEx();

                    var getKeyFunc = keySelector.Compile();

                    list.ForEach(e =>
                    {
                        e.SetId(dictionary.GetOrThrow(getKeyFunc(e)));
                        e.SetIsNew(false);
                    });

                    BulkInsertMLists(list, copyOptions, timeout, message);

                    GraphExplorer.CleanModifications(GraphExplorer.FromRoots(list));

                    return(tr.Commit(rowNum));
                }
        }
Example #3
0
        public static void Save(Entity[] entities)
        {
            if (entities == null || entities.Any(e => e == null))
            {
                throw new ArgumentNullException("entity");
            }

            using (var log = HeavyProfiler.LogNoStackTrace("PreSaving"))
            {
                Schema schema = Schema.Current;
                DirectedGraph <Modifiable> modifiables = PreSaving(() => GraphExplorer.FromRoots(entities));

                HashSet <Entity> wasNew          = modifiables.OfType <Entity>().Where(a => a.IsNew).ToHashSet(ReferenceEqualityComparer <Entity> .Default);
                HashSet <Entity> wasSelfModified = modifiables.OfType <Entity>().Where(a => a.Modified == ModifiedState.SelfModified).ToHashSet(ReferenceEqualityComparer <Entity> .Default);

                log.Switch("Integrity");

                var error = GraphExplorer.FullIntegrityCheck(modifiables);
                if (error != null)
                {
#if DEBUG
                    throw new IntegrityCheckException(error.WithEntities(modifiables));
#else
                    throw new IntegrityCheckException(error);
#endif
                }

                log.Switch("Graph");

                GraphExplorer.PropagateModifications(modifiables.Inverse());

                //colapsa modifiables (collections and embeddeds) keeping indentifiables only
                DirectedGraph <Entity> identifiables = GraphExplorer.ColapseIdentifiables(modifiables);

                foreach (var node in identifiables)
                {
                    schema.OnSaving(node);
                }

                //Remove all the edges that doesn't mean a dependency
                identifiables.RemoveEdges(identifiables.Edges.Where(e => !e.To.IsNew).ToList());

                //Remove all the nodes that are not modified
                List <Entity> notModified = identifiables.Where(node => !node.IsGraphModified).ToList();

                notModified.ForEach(node => identifiables.RemoveFullNode(node, None));

                log.Switch("SaveGroups");

                SaveGraph(schema, identifiables);

                foreach (var node in identifiables)
                {
                    schema.OnSaved(node, new SavedEventArgs
                    {
                        IsRoot          = entities.Contains(node),
                        WasNew          = wasNew.Contains(node),
                        WasSelfModified = wasSelfModified.Contains(node),
                    });
                }

                EntityCache.Add(identifiables);
                EntityCache.Add(notModified);

                GraphExplorer.CleanModifications(modifiables);
            }
        }