예제 #1
0
        public static Level1 GetSample()
        {
            Level1 level1 = new Level1();

            level1.Value  = "test";
            level1.Id     = levelIds[0]++;
            level1.levels = new List <Level2>();

            for (int i = 0; i < 5; i++)
            {
                Level2 curLevel2 = new Level2();
                level1.levels.Add(curLevel2);
                curLevel2.Id     = levelIds[1]++;
                curLevel2.Value  = "test" + i.ToString();
                curLevel2.levels = new List <Level3>();

                for (int j = 0; j < 5; j++)
                {
                    Level3 curLevel3 = new Level3();
                    curLevel2.levels.Add(curLevel3);
                    curLevel3.Id     = levelIds[2]++;
                    curLevel3.Value  = "test" + j.ToString();
                    curLevel3.levels = new List <Level4>();

                    for (int k = 0; k < 10; k++)
                    {
                        Level4 curLevel4 = new Level4();
                        curLevel3.levels.Add(curLevel4);
                        curLevel4.Id     = levelIds[3]++;
                        curLevel4.Value  = "test" + k.ToString();
                        curLevel4.levels = new List <Level5>();

                        for (int l = 0; l < 10; l++)
                        {
                            Level5 curLevel5 = new Level5();
                            curLevel4.levels.Add(curLevel5);
                            curLevel5.Id    = levelIds[4]++;
                            curLevel5.Value = "test" + l.ToString();
                        }
                    }
                }
            }

            return(level1);
        }
예제 #2
0
        public static void BatchTest()
        {
            Level1 level1 = new Level1();

            level1.Value  = "test";
            level1.Id     = levelIds[0]++;
            level1.levels = new List <Level2>();
            long ms = 0;

            using (new MetricTracker("Simple save test", t => ms = t))
            {
                for (int i = 0; i < 1296; i++)
                {
                    Level2 curLevel2 = new Level2();
                    level1.levels.Add(curLevel2);
                    curLevel2.Id     = levelIds[1]++;
                    curLevel2.Value  = "test" + i.ToString();
                    curLevel2.levels = new List <Level3>();
                }
                using (Levels context = new Levels())
                {
                    context.Level1.Add(level1);
                    context.SaveChanges();
                }
            }

            using (new MetricTracker("Complex save test - preparation ", t => ms = t))
            {
                level1 = GetSample();
            }

            using (Levels context = new Levels())
                using (new MetricTracker("Complex save test - Save", t => ms = t))
                {
                    context.Level1.Add(level1);
                    context.SaveChanges();
                }
        }
예제 #3
0
        /// <summary>
        /// Returns generated Id.
        /// </summary>
        /// <returns></returns>
        public static Int32 Write_Level1(Level1 level1, SqlCeConnection conn)
        {
            Dictionary <string, Int32> idBag = new Dictionary <string, int>();

            // Level1
            using (SqlCeCommand cmd = new SqlCeCommand(level1.GetType().Name, conn))
            {
                cmd.CommandType = System.Data.CommandType.TableDirect;
                using (SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable))
                {
                    SqlCeUpdatableRecord r = rs.CreateRecord();
                    r.SetString(1, level1.Value);                                         // Payload
                    rs.Insert(r, DbInsertOptions.PositionOnInsertedRow);
                    idBag[level1.GetType().Name] = rs.GetInt32(0);
                }
            }

            foreach (var level2 in level1.levels)                                         // Payload
            {
                // Level2
                using (SqlCeCommand cmd = new SqlCeCommand(level2.GetType().Name, conn))
                {
                    cmd.CommandType = System.Data.CommandType.TableDirect;
                    using (SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable))
                    {
                        SqlCeUpdatableRecord r = rs.CreateRecord();
                        r.SetInt32(GetOrdinal(r, level2.GetType(), level1.GetType()), idBag[level1.GetType().Name]);        // foreign key
                        r.SetString(1, level2.Value);                                                                       // payload
                        rs.Insert(r, DbInsertOptions.PositionOnInsertedRow);
                        idBag[level2.GetType().Name] = rs.GetInt32(0);
                    }
                }

                foreach (var level3 in level2.levels)
                {
                    using (SqlCeCommand cmd = new SqlCeCommand(level3.GetType().Name, conn))
                    {
                        cmd.CommandType = System.Data.CommandType.TableDirect;
                        using (SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable))
                        {
                            SqlCeUpdatableRecord r = rs.CreateRecord();
                            r.SetInt32(GetOrdinal(r, level3.GetType(), level2.GetType()), idBag[level2.GetType().Name]);
                            r.SetString(1, level3.Value);
                            rs.Insert(r, DbInsertOptions.PositionOnInsertedRow);
                            idBag[level3.GetType().Name] = rs.GetInt32(0);
                        }
                    }

                    foreach (var level4 in level3.levels)
                    {
                        using (SqlCeCommand cmd = new SqlCeCommand(level4.GetType().Name, conn))
                        {
                            cmd.CommandType = System.Data.CommandType.TableDirect;
                            using (SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable))
                            {
                                SqlCeUpdatableRecord r = rs.CreateRecord();
                                r.SetInt32(GetOrdinal(r, level4.GetType(), level3.GetType()), idBag[level3.GetType().Name]);
                                r.SetString(1, level4.Value);
                                rs.Insert(r, DbInsertOptions.PositionOnInsertedRow);
                                idBag[level4.GetType().Name] = rs.GetInt32(0);
                            }
                        }

                        foreach (var level5 in level4.levels)
                        {
                            using (SqlCeCommand cmd = new SqlCeCommand(level5.GetType().Name, conn))
                            {
                                cmd.CommandType = System.Data.CommandType.TableDirect;
                                using (SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable))
                                {
                                    SqlCeUpdatableRecord r = rs.CreateRecord();
                                    r.SetInt32(GetOrdinal(r, typeof(Level5), typeof(Level4)), idBag[level4.GetType().Name]);
                                    r.SetString(1, level5.Value);
                                    rs.Insert(r, DbInsertOptions.PositionOnInsertedRow);
                                }
                            }
                        }
                    }
                }
            }

            return(idBag[level1.GetType().Name]);
        }