4.9.9.11 试用版本 返回

求助
4 299

试用版本请到体验群获取DLL,如何加体验群请到普通群向群员要二维码加入

底层更新比较大所以采用了试用模式

1.主要更新内容第一个SqlSugarClient支持了线程安全模式,第二个是多库管理和多库分布式事务 下面是用例

        public static void Init()
        {
            SingletonPattern();//单例
            DistributedTransactionExample();//分布式事务
        }

        private static void SingletonPattern()
        {
            Console.WriteLine("");
            Console.WriteLine("#### Singleton Pattern Start ####");
            Console.WriteLine("Db_Id:" + singleDb.ContextID);
            Console.WriteLine("Db_Id:" + singleDb.ContextID);
            var task = new Task(() =>
            {
                Console.WriteLine("Task DbId:" + singleDb.ContextID);
                new Task(() =>
                {
                    Console.WriteLine("_Task_Task DbId:" + singleDb.ContextID);
                    Console.WriteLine("_Task_Task DbId:" + singleDb.ContextID);

                }).Start();
                Console.WriteLine("Task DbId:" + singleDb.ContextID);
            });
            task.Start();
            task.Wait();
            System.Threading.Thread.Sleep(500);
            Console.WriteLine(string.Join(",", singleDb.TempItems.Keys));

            Console.WriteLine("#### Singleton Pattern end ####");
        }

        static SqlSugarClient singleDb = new SqlSugarClient(
            new ConnectionConfig()
            {
                ConfigId = 1,
                DbType = DbType.SqlServer,
                ConnectionString = Config.ConnectionString,
                InitKeyType = InitKeyType.Attribute,
                IsAutoCloseConnection=true,
                AopEvents = new AopEvents()
                {
                    OnLogExecuting = (sql, p) => { Console.WriteLine(sql); }
                }
            });


        private static void DistributedTransactionExample()
        {
            Console.WriteLine("");
            Console.WriteLine("#### Distributed TransactionExample Start ####");
            SqlSugarClient db = new SqlSugarClient(new List<ConnectionConfig>()
            {
                new ConnectionConfig(){ ConfigId=1, DbType=DbType.SqlServer, ConnectionString=Config.ConnectionString,InitKeyType=InitKeyType.Attribute,IsAutoCloseConnection=true },
                new ConnectionConfig(){ ConfigId=2, DbType=DbType.MySql, ConnectionString=Config.ConnectionString4 ,InitKeyType=InitKeyType.Attribute ,IsAutoCloseConnection=true}
            });

            //use first(SqlServer)
            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 mysql
            db.ChangeDatabase(it => it.DbType == DbType.MySql);
            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());

            //SqlServer
            db.ChangeDatabase(it => it.DbType == DbType.SqlServer);//use sqlserver
            try
            {
                db.BeginAllTran();

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

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

                throw new Exception();
                db.CommitAllTran();
            }
            catch
            {
                db.RollbackAllTran();
                Console.WriteLine("---Roll back");
                db.ChangeDatabase(it => it.DbType == DbType.SqlServer);//use sqlserver
                Console.WriteLine(db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable<Order>().Count());

                db.ChangeDatabase(it => it.DbType == DbType.MySql);//use mysql
                Console.WriteLine(db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable<Order>().Count());
            }

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

2.更新插入API修改美化将不合理的API全部加上过期标签

更新最新的API参考 https://github.com/sunkaixuan/SqlSugar/wiki/2.Updateable




热忱回答4

  • using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;


    namespace OrmTest

    {


        public class Order

        {

            [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]

            public int Id { get; set; }


            public string Name { get; set; }

            public decimal Price { get; set; }

            [SqlSugar.SugarColumn(IsNullable =true)]

            public DateTime CreateTime { get; set; }

        }

    }


    0 回复
  • using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;


    namespace OrmTest

    {

        [SqlSugar.SugarTable("OrderDetail")]

        public class OrderItem

        {

            [SqlSugar.SugarColumn(IsPrimaryKey =true, IsIdentity =true)]

            public int ItemId { get; set; }

            public int OrderId { get; set; }

            public decimal? Price { get; set; }

            [SqlSugar.SugarColumn(IsNullable = true)]

            public DateTime? CreateTime { get; set; }

        }

    }


    0 回复
  • 试用一下。

    0 回复
  • UniqueIndexUniqueIndexUniqueIndex

    0 回复

版块

学习文档

最新会员

发布达人

回贴达人