Ejemplo n.º 1
0
        public static void TrackingTest()
        {
            MsSqlContext context = new MsSqlContext(DbHelper.ConnectionString);

            object ret = null;

            var q = context.Query <User>();

            q = q.AsTracking();

            User user = q.First();

            ret = context.Update(user);

            Console.WriteLine(ret);

            context.TrackEntity(user);
            user.Name   = user.Name + "1";
            user.Age    = user.Age;
            user.Gender = null;
            ret         = context.Update(user);

            Console.WriteLine(ret);

            ret = context.Update(user);
            Console.WriteLine(ret);

            ConsoleHelper.WriteLineAndReadKey();
        }
Ejemplo n.º 2
0
        public static void Update()
        {
            object ret = null;

            ret = context.Update <TestEntity>(a => a.Id == 1, a => new TestEntity()
            {
                F_Byte = 1
                ,
                F_Int16 = 16
                ,
                F_Int32 = 32
                ,
                F_Int64 = 64
                ,
                F_Double = 1.123456
                ,
                F_Float = 1.1234F
                ,
                F_Decimal = 1.12345678M
                ,
                F_Bool = true
                ,
                F_DateTime = DateTime.Now
                ,
                F_Guid = Guid.NewGuid()
                ,
                F_String = "12345"
            });

            Console.WriteLine(ret);

            var entity = new TestEntity();

            entity.Id         = 1;
            entity.F_Byte     = 1;
            entity.F_Int16    = 16;
            entity.F_Int32    = 32;
            entity.F_Int64    = 64;
            entity.F_Double   = 1.123456;
            entity.F_Float    = 1.1234F;
            entity.F_Decimal  = 1.12345678M;
            entity.F_Bool     = true;
            entity.F_DateTime = DateTime.Now;
            entity.F_Guid     = Guid.NewGuid();
            entity.F_String   = "12345";

            ret = context.Update(entity);

            Console.WriteLine(ret);

            context.TrackEntity(entity);


            entity.F_String = "lu";
            ret             = context.Update(entity);

            Console.WriteLine(ret);

            ConsoleHelper.WriteLineAndReadKey();
        }
Ejemplo n.º 3
0
        public static void Update()
        {
            context.Update <User>(a => a.Id == 1, a => new User()
            {
                Name = a.Name, Age = a.Age + 1, Gender = Gender.Man, OpTime = DateTime.Now
            });

            /*
             * UPDATE [Users] SET [Name]=[Users].[Name],[Age]=([Users].[Age] + 1),[Gender]=1,[OpTime]=GETDATE() WHERE [Users].[Id] = 1
             */

            //批量更新
            //给所有女性年轻 1 岁
            context.Update <User>(a => a.Gender == Gender.Woman, a => new User()
            {
                Age = a.Age - 1, OpTime = DateTime.Now
            });

            /*
             * UPDATE [Users] SET [Age]=([Users].[Age] - 1),[OpTime]=GETDATE() WHERE [Users].[Gender] = 2
             */

            User user = new User();

            user.Id     = 1;
            user.Name   = "lu";
            user.Age    = 28;
            user.Gender = Gender.Man;
            user.OpTime = DateTime.Now;

            context.Update(user); //会更新所有映射的字段

            /*
             * String @P_0 = 'lu';
             * Gender @P_1 = Man;
             * Int32 @P_2 = 28;
             * Nullable<Int32> @P_3 = NULL;
             * DateTime @P_4 = '2016/8/26 18:18:36';
             * Int32 @P_5 = 1;
             * UPDATE [Users] SET [Name]=@P_0,[Gender]=@P_1,[Age]=@P_2,[CityId]=@P_3,[OpTime]=@P_4 WHERE [Users].[Id] = @P_5
             */


            /*
             * 支持只更新属性值已变的属性
             */

            context.TrackEntity(user); //在上下文中跟踪实体
            user.Name = user.Name + "1";
            context.Update(user);      //这时只会更新被修改的字段

            /*
             * String @P_0 = 'lu1';
             * Int32 @P_1 = 1;
             * UPDATE [Users] SET [Name]=@P_0 WHERE [Users].[Id] = @P_1
             */

            ConsoleHelper.WriteLineAndReadKey();
        }
Ejemplo n.º 4
0
        public static void TrackingTest()
        {
            MsSqlContext context = new MsSqlContext(DbHelper.ConnectionString);

            object ret = null;

            var q = context.Query<User>();
            q = q.AsTracking();

            User user = q.First();
            ret = context.Update(user);

            Console.WriteLine(ret);

            context.TrackEntity(user);
            user.Name = user.Name + "1";
            user.Age = user.Age;
            user.Gender = null;
            ret = context.Update(user);

            Console.WriteLine(ret);

            ret = context.Update(user);
            Console.WriteLine(ret);

            ConsoleHelper.WriteLineAndReadKey();
        }
Ejemplo n.º 5
0
        public static void UpdateTest1()
        {
            object ret = null;

            int r = -1;

            MsSqlContext context = new MsSqlContext(DbHelper.ConnectionString);

            var u = context.Query <User>().AsTracking().First(a => a.Id == 3);

            context.Update <User>(a => a.Id == 1, a => new User()
            {
                Name = a.Name, Age = a.Age + 100, Gender = Gender.Man, OpTime = DateTime.Now
            });

            /*
             * UPDATE [Users] SET [Name]=[Users].[Name],[Age]=([Users].[Age] + 100),[Gender]=1,[OpTime]=GETDATE() WHERE [Users].[Id] = 1
             */

            //批量更新
            //给所有女性年轻 10 岁
            context.Update <User>(a => a.Gender == Gender.Woman, a => new User()
            {
                Age = a.Age - 10, OpTime = DateTime.Now
            });

            /*
             * UPDATE [Users] SET [Age]=([Users].[Age] - 10),[OpTime]=GETDATE() WHERE [Users].[Gender] = 2
             */

            User user = new User();

            user.Id     = 1;
            user.Name   = "lu";
            user.Age    = 28;
            user.Gender = Gender.Man;
            user.OpTime = DateTime.Now;

            context.Update(user); //会更新所有映射的字段

            /*
             * String @P_0 = "lu";
             * Gender @P_1 = Man;
             * Int32 @P_2 = 28;
             * Nullable<Int32> @P_3 = NULL;
             * DateTime @P_4 = "2016/7/8 11:28:27";
             * Int32 @P_5 = 1;
             * UPDATE [Users] SET [Name]=@P_0,[Gender]=@P_1,[Age]=@P_2,[CityId]=@P_3,[OpTime]=@P_4 WHERE [Users].[Id] = @P_5
             */

            /*
             * 支持只更新属性值已变的属性
             */

            context.TrackEntity(user); //在上下文中跟踪实体
            user.Name = user.Name + "1";
            context.Update(user);      //这时只会更新被修改的字段

            /*
             * String @P_0 = "lu1";
             * Int32 @P_1 = 1;
             * UPDATE [Users] SET [Name]=@P_0 WHERE [Users].[Id] = @P_1
             */


            ConsoleHelper.WriteLineAndReadKey();
        }