コード例 #1
0
        /// <summary>
        /// 测试 AUTO_INCREMENT 在系统中,是否会影响 EF 的运作。
        /// </summary>
        public static void DoTest()
        {
            using (TestEntities context = new TestEntities())
            {
                Console.WriteLine();
                Console.WriteLine("测试 AUTO_INCREMENT Start!");



                Console.WriteLine("首先测试  执行插入处理!");
                try
                {
                    test_auto_increment_tab test1 = new test_auto_increment_tab()
                    {
                        value = "测试"
                    };

                    context.test_auto_increment_tab.AddObject(test1);
                    context.SaveChanges();
                    Console.WriteLine("执行插入成功!");



                    Console.WriteLine("数据库那里产生了自增的 AUTO_INCREMENT 数值, 同时更新到当前这个字段上.");
                    Console.WriteLine("test1 的 id = {0}", test1.id);


                    Console.WriteLine("尝试再检索.");
                    var query =
                        from data in context.test_auto_increment_tab
                        where data.value == "测试"
                        select data;

                    foreach (test_auto_increment_tab t in query)
                    {
                        Console.WriteLine("id = {0};  value = {1} ", t.id, t.value);
                        context.test_auto_increment_tab.DeleteObject(t);
                    }

                    context.SaveChanges();
                    Console.WriteLine("执行删除成功!");

                }
                catch (Exception ex)
                {
                    Console.WriteLine("执行失败!");

                    Console.WriteLine(ex.Message);
                }



                Console.WriteLine("测试 AUTO_INCREMENT Finish!");
                Console.WriteLine();
            }
        }
コード例 #2
0
        /// <summary>
        /// 测试调用 testProc 存储过程.
        /// </summary>
        private static void TestProc()
        {
            using (TestEntities context = new TestEntities())
            {
                Console.WriteLine("调用 testProc 存储过程!  该存储过程 无参数, 返回一个2行2列的结果集 ");

                var query = context.TestProc();

                foreach (testprocresult oneResult in query)
                {
                    Console.WriteLine("A={0},  B={1}",  oneResult.A,  oneResult.B);
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// 测试调用 HelloWorld 存储过程.
        /// </summary>
        private static void TestHelloWorld()
        {
            using (TestEntities context = new TestEntities())
            {
                Console.WriteLine("调用 HelloWorld2 存储过程!  该存储过程 3个参数, 1个输入, 2个输出, 返回一个单行的结果集 ");

                ObjectParameter p1 = new ObjectParameter("vOutValue", "");
                ObjectParameter p2 = new ObjectParameter("vInOutValue", "");

                ObjectResult<string> result = context.HelloWorld2("Test", p1, p2);


                Console.WriteLine(result.FirstOrDefault());
                Console.WriteLine(p1.Value);
                Console.WriteLine(p2.Value);
            }
        }
コード例 #4
0
        /// <summary>
        /// 测试.
        /// </summary>
        public static void DoTest()
        {

            using (TestEntities context = new TestEntities())
            {
                Console.WriteLine();
                Console.WriteLine("测试 中文汉字输入 Start!");


                test_tab testData = new test_tab()
                {
                     value1 = "测试中文1",
                     value2 = "测试中文2",
                };

                context.test_tab.AddObject(testData);
                context.SaveChanges();
                Console.WriteLine("执行插入成功!");



                Console.WriteLine("尝试再检索.");
                var query =
                    from data in context.test_tab
                    where data.value1 == "测试中文1"
                    select data;


                foreach (test_tab t in query)
                {
                    Console.WriteLine("id = {0};  value1 = {1};  value2 = {2} ", t.id, t.value1, t.value2);
                    context.test_tab.DeleteObject(t);
                }

                context.SaveChanges();
                Console.WriteLine("执行删除成功!");


                Console.WriteLine("测试 中文汉字输入 Finish!");
            }


        }
コード例 #5
0
        /// <summary>
        /// 测试.
        /// </summary>
        public static void DoTest()
        {


            // 注意: 普通的 延迟加载
            // SQL Server 是通过 数据库的连接字符串中的默认的  MultipleActiveResultSets = True 
            // 来实现不报 {"There is already an open DataReader associated with this Connection which must be closed first."}
            // 这样的错误的.

            // 但是 MySQL 不支持  MultipleActiveResultSets = True   这样的设置.
            // 因此,这里无法简单的像 SQL Server 那样的, 实现 延迟加载.
            // 非延迟加载情况下, 使用 load() 也会发生异常.
            // 只能通过  include  的方式, 来进行相关的处理.
           


            using (TestEntities context = new TestEntities())
            {
                Console.WriteLine("测试 使用 延迟加载!");

                // 使用 延迟加载.
                context.ContextOptions.LazyLoadingEnabled = true;

                try
                {
                    Console.WriteLine("### 开始测试 一对多 的延迟加载! ###");

                    // 这里主动查询 test_main
                    foreach (test_main m in context.test_main.Where(p => p.id < 100))
                    {
                        Console.WriteLine("Main = {0}:{1}", m.id, m.value);
                        // 下面使用 延迟加载, 自动取查询 test_sub
                        if (m.test_sub != null)
                        {
                            foreach (test_sub s in m.test_sub)
                            {
                                Console.WriteLine("----- Sub = {0}:{1}", s.id, s.value);
                            }
                        }


                        Console.WriteLine("========================================");

                        // 测试三层的.
                        if (m.test_sub_s != null)
                        {
                            foreach (test_sub_s s in m.test_sub_s)
                            {
                                Console.WriteLine("----- Sub_S = {0}:{1}", s.id, s.value);

                                if (s.test_sub_of_sub != null)
                                {
                                    foreach (test_sub_of_sub ss in s.test_sub_of_sub)
                                    {
                                        Console.WriteLine("---------- Sub_S = {0}:{1}", ss.id, ss.value);
                                    }
                                }

                            }
                        }
                    }



                    Console.WriteLine("### 开始测试 多对多 的延迟加载! ###");

                    foreach (test_student m in context.test_student)
                    {
                        Console.WriteLine("学生 = {0}:{1}", m.student_code, m.student_name);
                        // 下面使用 延迟加载, 自动取查询 test_score
                        if (m.test_score != null)
                        {
                            foreach (test_score s in m.test_score)
                            {
                                Console.WriteLine("----- 成绩 = {0}:{1}:{2}", s.course_code, s.test_course.course_name, s.score_value);
                            }
                        }
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine("延迟加载失败了!");
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(ex.InnerException.Message);
                }
            }







            
            Console.WriteLine();
            using (TestEntities context = new TestEntities())
            {
                Console.WriteLine("测试 不使用 延迟加载!");
                // 不使用 延迟加载.
                context.ContextOptions.LazyLoadingEnabled = false;


                try
                {
                    Console.WriteLine("普通方式查询 test_main!");
                    // 这里主动查询 test_main
                    foreach (test_main m in context.test_main.Where(p => p.id < 100))
                    {
                        Console.WriteLine("Main = {0}:{1}", m.id, m.value);
                        if (m.test_sub != null)
                        {
                            // 注意:
                            //    使用 非延迟加载的, 结果不是 NULL, 是一个空白列表.
                            foreach (test_sub s in m.test_sub)
                            {
                                Console.WriteLine("----- Sub = {0}:{1}", s.id, s.value);
                            }
                        }
                        else
                        {
                            Console.WriteLine("m.test_sub  is  null...");
                        }


                        // 三层的这里就不测试了......


                        Console.WriteLine("测试 IsLoaded 的判断");

                        // 在 不使用 延迟加载 的情况下,
                        // 可以通过  IsLoaded / Load 来手动加载.
                        if (!m.test_sub.IsLoaded)
                        {
                            m.test_sub.Load();
                            foreach (test_sub s in m.test_sub)
                            {
                                Console.WriteLine("----- Sub = {0}:{1}", s.id, s.value);
                            }

                        }


                        // 三层的这里就不测试了......
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("非延迟加载下, 使用 Load()  失败了!");
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(ex.InnerException.Message);
                }

            }






            using (TestEntities context = new TestEntities())
            {



                Console.WriteLine();
                Console.WriteLine("Include 方式查询 test_main!");


                // 这里使用 贪婪加载.
                // 请注意:
                // 1、如果一个 主表, 同时关联有很多套子表, 那么可以通过  Include("子表1").Include("子表2").Include("子表3") 的方式, 来进行加载.
                // 2、当表的层级结构是多层的时候,例如 爷爷-父亲-孙子 , 那么需要通过 爷爷.Include("父亲.孙子") 的方式来处理。
                foreach (test_main m in context.test_main.Include("test_sub").Include("test_sub_s.test_sub_of_sub").Where(p => p.id < 100))
                {
                    Console.WriteLine("Main = {0}:{1}", m.id, m.value);
                    if (m.test_sub != null)
                    {
                        foreach (test_sub s in m.test_sub)
                        {
                            Console.WriteLine("----- Sub = {0}:{1}", s.id, s.value);
                        }
                    }
                    else
                    {
                        Console.WriteLine("m.test_sub  is  null...");
                    }




                    Console.WriteLine("========================================");

                    // 测试三层的.
                    if (m.test_sub_s != null)
                    {
                        foreach (test_sub_s s in m.test_sub_s)
                        {
                            Console.WriteLine("----- Sub_S = {0}:{1}", s.id, s.value);

                            if (s.test_sub_of_sub != null)
                            {
                                foreach (test_sub_of_sub ss in s.test_sub_of_sub)
                                {
                                    Console.WriteLine("---------- Sub_S = {0}:{1}", ss.id, ss.value);
                                }
                            }

                        }
                    }
                }




                Console.WriteLine();
                Console.WriteLine("多对多关系,Include 多层次 查询!");

                foreach (test_student m in context.test_student.Include("test_score.test_course"))
                {
                    Console.WriteLine("学生 = {0}:{1}", m.student_code, m.student_name);
                    // 下面使用 延迟加载, 自动取查询 test_score
                    if (m.test_score != null)
                    {
                        foreach (test_score s in m.test_score)
                        {
                            Console.WriteLine("----- 成绩 = {0}:{1}:{2}", s.course_code, s.test_course.course_name, s.score_value);
                        }
                    }



                }


                Console.WriteLine();
                Console.WriteLine("多对多关系,Include 多表 查询!");
                foreach (test_score s in context.test_score.Include("test_student").Include("test_course"))
                {

                    Console.WriteLine("学生 = {0}  课程 = {1}  成绩 = {2} ",
                        s.test_student.student_name,
                        s.test_course.course_name,
                        s.score_value);
                }


            }


        }