Exemplo n.º 1
0
        public static void Init()
        {
            Console.WriteLine("");
            Console.WriteLine("#### Deleteable Start ####");

            AgileClient db = new AgileClient(new ConnectionConfig()
            {
                DbType                = DatabaseType.SqlServer,
                ConnectionString      = Config.ConnectionString,
                InitKeyType           = InitKeyType.Attribute,
                IsAutoCloseConnection = true,
                AopEvents             = new AopEvents
                {
                    OnLogExecuting = (sql, p) =>
                    {
                        Console.WriteLine(sql);
                        Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
                    }
                }
            });

            //by entity
            db.Deleteable <Order>().Where(new Order()
            {
                Id = 1111
            }).ExecuteCommand();

            //by primary key
            db.Deleteable <Order>().In(1111).ExecuteCommand();

            //by primary key array
            db.Deleteable <Order>().In(new int[] { 1111, 2222 }).ExecuteCommand();

            //by expression
            db.Deleteable <Order>().Where(it => it.Id == 11111).ExecuteCommand();

            Console.WriteLine("#### Deleteable End ####");
        }
Exemplo n.º 2
0
        private static void DistributedTransactionExample()
        {
            Console.WriteLine("");
            Console.WriteLine("#### Distributed TransactionExample Start ####");
            AgileClient db = new AgileClient(new List <ConnectionConfig>()
            {
                new ConnectionConfig()
                {
                    ConfigId = "1", DbType = DatabaseType.SqlServer, ConnectionString = Config.ConnectionString, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true
                },
                new ConnectionConfig()
                {
                    ConfigId = "2", DbType = DatabaseType.SqlServer, ConnectionString = Config.ConnectionString2, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true
                }
            });

            //use db1
            db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Order), typeof(OrderItem));//
            db.Insertable(new Order()
            {
                Name = "order1", CreateTime = DateTime.Now
            }).ExecuteCommand();
            Console.WriteLine(db.CurrentConnectionConfig.DbType + ":" + db.Queryable <Order>().Count());

            //use db2
            db.ChangeDatabase("2");
            db.DbMaintenance.CreateDatabase();//Create Database2
            db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Order), typeof(OrderItem));
            db.Insertable(new Order()
            {
                Name = "order1", CreateTime = DateTime.Now
            }).ExecuteCommand();
            Console.WriteLine(db.CurrentConnectionConfig.DbType + ":" + db.Queryable <Order>().Count());

            // Example 1
            Console.WriteLine("Example 1");
            try
            {
                db.BeginTran();

                db.ChangeDatabase("1");//use db1
                db.Deleteable <Order>().ExecuteCommand();
                Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());

                db.ChangeDatabase("2");//use db2
                db.Deleteable <Order>().ExecuteCommand();
                Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());

                throw new Exception();
                db.CommitTran();
            }
            catch
            {
                db.RollbackTran();
                Console.WriteLine("---Roll back");
                db.ChangeDatabase("1");//use db1
                Console.WriteLine(db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());

                db.ChangeDatabase("2");//use db2
                Console.WriteLine(db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());
            }



            // Example 2
            Console.WriteLine("Example 2");

            var result = db.UseTran(() =>
            {
                db.ChangeDatabase("1");//use db1
                db.Deleteable <Order>().ExecuteCommand();
                Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());

                db.ChangeDatabase("2");//use db2
                db.Deleteable <Order>().ExecuteCommand();
                Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());
                throw new Exception("");
            });

            if (result.IsSuccess == false)
            {
                Console.WriteLine("---Roll back");
                db.ChangeDatabase("1");//use db1
                Console.WriteLine(db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());

                db.ChangeDatabase("2");//use db2
                Console.WriteLine(db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());
            }

            // Example 3
            Console.WriteLine("Example 3");

            var result2 = db.UseTranAsync(() =>
            {
                db.ChangeDatabase("1");//use db1
                db.Deleteable <Order>().ExecuteCommand();
                Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());

                db.ChangeDatabase("2");//use db2
                db.Deleteable <Order>().ExecuteCommand();
                Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());
                throw new Exception("");
            });

            result2.Wait();
            if (result.IsSuccess == false)
            {
                Console.WriteLine("---Roll back");
                db.ChangeDatabase("1");//use sqlserver
                Console.WriteLine(db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());

                db.ChangeDatabase("2");//use mysql
                Console.WriteLine(db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());
            }

            Console.WriteLine("#### Distributed TransactionExample End ####");
        }