Beispiel #1
0
        public void TestAccessOrm()
        {
            //ORM数据映射
            DbConfig.UseDefaultConfig(new TModelDbConfig(GetDbPath()));
            Console.WriteLine("Start loadding...");
            Console.WriteLine(new cms_category().Query(m => m.name == "城市").ToCount());
            var cat = new cms_category().Query(m => m.name == "城市").SortAsc(m => m.name).ToModel();

            Console.WriteLine(cat.name);

            //设置只更新部分
            //cat.SetPartHandled();
            //cat.description = "test";
            //cat.Update(m=>m.id == 1);

            Console.WriteLine(cat.ToValue(m => m.name));
            Console.WriteLine(new cms_category().Query(m => m.name == "城市").ToList()[0].name);
            Console.WriteLine(new cms_category().Query(m => m.name == "城市" && m.id > 0 && m.name == "" || (m.id == 0 || m.name == "")).ToCount());
            //指定条件规则查询
            Console.WriteLine(new cms_category().Query(m => (m.name == "城市" && (m.id > 0 || m.name == "")) || (m.id == 0 || m.name == "")).ToCount());

            var cityList = new List <string> {
                "城市", "b", "c"
            };
            var layer = new LayerModel {
                List = cityList
            };

            Console.WriteLine(new cms_category().Query(m => m.name == "城市" || cityList.Contains(m.name) || m.parent_id == Status.Success).ToCount());
            Console.WriteLine(new cms_category().Query(m => m.name == "城市" || layer.List.Contains(m.name)).ToCount());


            //获取全部
            var datsList = new cms_category().Query().ToList();

            Console.WriteLine(datsList.Count);
            //获取N条
            datsList = new cms_category().Query().ToList(6);
            Console.WriteLine(datsList.Count);
            //获取部分
            var partList = new cms_category().Query().ToPartList(6, "id", "name").Select(m => new cms_category
            {
                id   = int.Parse(m[0]),
                name = m[1]
            }).ToList();

            Console.WriteLine(partList.Count);
            //分页查询
            var mapper    = new cms_category().Query();
            var dataCount = mapper.ToCount();

            datsList = mapper.ToList(20, 1, dataCount);
            Console.WriteLine(datsList.Count);
            //条件拼接查询
            mapper.And(m => m.name == "test")
            .And(m => m.id > 0)
            .Or(m => m.parent_id > 0);
            mapper.Or(m => m.parent_id > 0);



            var channels = new cms_channel().Query().ToList();

            Console.WriteLine(channels.Count);
            var grade = new ucl_grade {
                id = 5
            };

            grade.grade_name = "新手1";
            var dal = new UclGradeDataAccess(grade);

            //保持数据库连接
            using (var db = new DbBuilder(new TModelDbConfig(GetDbPath())).KeepConnect())
            {
                //使用数据库db操作并跟踪实体修改状态
                dal.UseDatabase(db).SetPartHandled();
                grade.grade      = 8;
                grade.grade_name = "新手";
                dal.Update();
            }
            //db销毁后重连数据库
            Console.WriteLine(dal.ToValue(m => m.grade_name));


            //使用事务(在事务中处理)
            using (var db = new DbBuilder(new TModelDbConfig(GetDbPath())).KeepConnect())
            {
                try
                {
                    db.BeginTransaction();
                    //TODO:something
                    //使用数据库db操作并跟踪实体修改状态
                    dal.UseDatabase(db).SetPartHandled();
                    grade.grade      = 8;
                    grade.grade_name = "新手";
                    dal.Update();
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                }
            }

            //使用事务(批处理事务)
            var parList = new List <DbParamInfo>();

            //添加到批处理事务中,如果执行失败则回滚事务
            parList.Add(dal.GetUpdateDbParamInfo().UseVerifyExecResult());
            //TODO:添加其他操作到parList
            var execCount = new DbBuilder(new TModelDbConfig(GetDbPath())).ExecuteSqlTran(parList);

            Console.WriteLine(execCount);
        }