private static void DbContext()
        {
            Console.WriteLine("");
            Console.WriteLine("#### DbContext Start ####");
            var insertObj = new Order {
                Name = "jack", CreateTime = DateTime.Now
            };
            var InsertObjs = new Order[] { insertObj };

            DbContext context = new DbContext();

            context.Db.CodeFirst.InitTables <Order, OrderItem, Custom>();//Create Tables
            ;
            var orderDb = context.OrderDb;

            //Select
            var data1 = orderDb.GetById(1);
            var data2 = orderDb.GetList();
            var data3 = orderDb.GetList(it => it.Id == 1);
            var data4 = orderDb.GetSingle(it => it.Id == 1);
            var p     = new PageModel()
            {
                PageIndex = 1, PageSize = 2
            };
            var data5 = orderDb.GetPageList(it => it.Name == "xx", p);

            Console.Write(p.PageCount);
            var data6 = orderDb.GetPageList(it => it.Name == "xx", p, it => it.Name, OrderByType.Asc);

            Console.Write(p.PageCount);
            List <IConditionalModel> conModels = new List <IConditionalModel>();

            conModels.Add(new ConditionalModel()
            {
                FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1", FieldValueConvertFunc = it => Convert.ToInt32(it)
            });                                                                                                                                                                  //id=1
            var data7 = orderDb.GetPageList(conModels, p, it => it.Name, OrderByType.Asc);

            orderDb.AsQueryable().Where(x => x.Id == 1).ToList();

            //Insert
            orderDb.Insert(insertObj);
            orderDb.InsertRange(InsertObjs);
            var id = orderDb.InsertReturnIdentity(insertObj);

            orderDb.AsInsertable(insertObj).ExecuteCommand();


            //Delete
            orderDb.Delete(insertObj);
            orderDb.DeleteById(11111);
            orderDb.DeleteById(new int[] { 1111, 2222 });
            orderDb.Delete(it => it.Id == 1111);
            orderDb.AsDeleteable().Where(it => it.Id == 1111).ExecuteCommand();

            //Update
            orderDb.Update(insertObj);
            orderDb.UpdateRange(InsertObjs);
            orderDb.Update(it => new Order()
            {
                Name = "a",
            }, it => it.Id == 1);
            orderDb.AsUpdateable(insertObj).UpdateColumns(it => new { it.Name }).ExecuteCommand();

            //Use Inherit DbContext
            OrderDal dal  = new OrderDal();
            var      data = dal.GetById(1);
            var      list = dal.GetList();

            Console.WriteLine("#### DbContext End ####");
        }
Пример #2
0
        public static void Init()
        {
            Console.WriteLine("");
            Console.WriteLine("#### Insertable Start ####");

            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                DbType                = DbType.Oracle,
                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)));
                    }
                }
            });

            var insertObj = new Order()
            {
                Id = 1, Name = "order1", Price = 0
            };
            var updateObjs = new List <Order> {
                new Order()
                {
                    Id = 11, Name = "order11", Price = 0
                },
                new Order()
                {
                    Id = 12, Name = "order12", Price = 0
                }
            };

            //Ignore  CreateTime
            db.Insertable(insertObj).IgnoreColumns(it => new { it.CreateTime }).ExecuteReturnIdentity();//get identity
            db.Insertable(insertObj).IgnoreColumns("CreateTime").ExecuteReturnIdentity();

            //Only  insert  Name and Price
            db.Insertable(insertObj).InsertColumns(it => new { it.Name, it.Price }).ExecuteReturnIdentity();
            db.Insertable(insertObj).InsertColumns("Name", "Price").ExecuteReturnIdentity();

            //ignore null columns
            db.Insertable(updateObjs).ExecuteCommand();//get change row count

            //Use Lock
            db.Insertable(insertObj).With(SqlWith.UpdLock).ExecuteCommand();
            Console.WriteLine("SubInsert Start");

            db.Insertable(new Order()
            {
                Name       = "订单 1",
                CustomId   = 1,
                Price      = 100,
                CreateTime = DateTime.Now,
                Id         = 0,
                Items      = new List <OrderItem>()
                {
                    new OrderItem()
                    {
                        CreateTime = DateTime.Now,
                        OrderId    = 0,
                        Price      = 1,
                        ItemId     = 1
                    },
                    new OrderItem()
                    {
                        CreateTime = DateTime.Now,
                        OrderId    = 0,
                        Price      = 2,
                        ItemId     = 2
                    }
                }
            })
            .AddSubList(it => it.Items.First().OrderId).ExecuteReturnPrimaryKey();


            SubNoIdentity(db);
            SubIdentity(db);
            Console.WriteLine("#### Insertable End ####");
        }