예제 #1
0
        public static void DeleteAllData(EntityApp app, IEnumerable <Type> inEntities = null, IEnumerable <Type> exceptEntities = null,
                                         IList <string> extraTablesToDelete           = null)
        {
            Util.Check(app.Status == EntityAppStatus.Connected, "EntityApp must be connected to database.");
            var db         = app.GetDefaultDatabase();
            var dbSettings = db.Settings;
            var driver     = dbSettings.Driver;

            if (inEntities == null)
            {
                inEntities = app.GetAllEntityTypes();
            }
            var typesToClear = new HashSet <Type>(inEntities);

            if (exceptEntities != null)
            {
                typesToClear.ExceptWith(exceptEntities);
            }
            // get existing tables; in unit tests it might happen that when we delete all for table that does not exist yet
            var modelLoader = driver.CreateDbModelLoader(dbSettings, app.ActivationLog);
            var schemas     = app.Areas.Select(a => a.Name).ToList();

            modelLoader.SetSchemasSubset(schemas);
            var oldModel      = modelLoader.LoadModel();
            var oldtableNames = new HashSet <string>(oldModel.Tables.Select(t => t.FullName), StringComparer.OrdinalIgnoreCase);
            //Figure out table/entity list and sort it
            var modelInfo = app.Model;
            var entList   = new List <EntityInfo>();

            foreach (var entType in typesToClear)
            {
                var entityInfo = modelInfo.GetEntityInfo(entType);
                if (entityInfo == null)
                {
                    continue;
                }
                entList.Add(entityInfo);
            }
            //sort in topological order
            entList.Sort((x, y) => x.TopologicalIndex.CompareTo(y.TopologicalIndex));
            // delete all one-by-one
            var conn = driver.CreateConnection(dbSettings.ConnectionString);

            conn.Open();
            var cmd = conn.CreateCommand();

            cmd.Transaction = conn.BeginTransaction();
            try {
                if (extraTablesToDelete != null)
                {
                    foreach (var extraTable in extraTablesToDelete)
                    {
                        if (oldtableNames.Contains(extraTable))
                        {
                            ExecuteDelete(driver, cmd, extraTable);
                        }
                    }
                }
                foreach (var entityInfo in entList)
                {
                    var table = db.DbModel.LookupDbObject <DbTableInfo>(entityInfo);
                    if (table == null)
                    {
                        continue;
                    }
                    if (!oldtableNames.Contains(table.FullName))
                    {
                        continue;
                    }
                    ExecuteDelete(driver, cmd, table.FullName);
                }
            } finally {
                cmd.Transaction.Commit();
                conn.Close();
            }
        }