public void Persistence()
        {
            using (var db = new ApiDb())
            {
                int value = 999999;

                //初次查询没有数据
                var re = db.Queryable <OperateTestModel>().Where(t => t.StringKey.StartsWith("AddTest")).ToList();
                Assert.Null(re);

                //add一条数据
                OperateTestModel model = new OperateTestModel
                {
                    IntKey    = value,
                    StringKey = "AddTest"
                };
                model.IntKey = value;
                db.Add <OperateTestModel>(model);

                //插入后查询有一条记录
                var re1 = db.Queryable <OperateTestModel>().Where(t => t.StringKey.StartsWith("AddTest")).ToList();
                Assert.Single(re1);
                Assert.Equal(value, re1.First().IntKey);

                //查询一条
                var entity = db.Queryable <OperateTestModel>().Where(t => t.IntKey == value).ToOne();
                Assert.NotNull(entity);
                Assert.Equal(value, entity.IntKey);

                //更新数据
                //entity.Id = value;   //自增的主键不应该被修改,如果用这种方式进行修改,给Id赋值就会导致修改不成功,因为条件是用第一个主键作为标识修改的
                entity.Key2            = value;
                entity.StringKey       = $"UpdateTest_{value}";
                entity.IntNullKey      = value;
                entity.DateTimeNullKey = DateTime.Now;
                entity.DateNullKey     = DateTime.Now.Date;
                entity.DoubleNullKey   = entity.IntNullKey;
                entity.FloatNullKey    = entity.IntNullKey;
                db.Update <OperateTestModel>(entity);

                var entity2 = db.Queryable <OperateTestModel>().Where(t => t.IntKey == value).ToOne();
                Assert.NotNull(entity2);
                Assert.Equal(value, entity2.IntNullKey);
                Assert.Equal($"UpdateTest_{value}", entity2.StringKey);

                //删除数据
                db.Delete <OperateTestModel>(t => t.IntKey == value);

                //删除后查询没有
                var re4 = db.Queryable <OperateTestModel>().Where(t => t.StringKey.StartsWith("AddTest")).ToList();
                Assert.Null(re4);
            }
        }
Beispiel #2
0
        public void Delete()
        {
            using (var db = new ApiDb())
            {
                db.Delete(new OperationTest
                {
                    Id = 1
                });

                Assert.Equal("DELETE t FROM OperateTest t WHERE t.Id = @tId", db.SqlStatement);
                Assert.Equal(new[] { "@tId" }, db.Parameters.Keys.ToArray());
            }
        }
        public void Persistence_DeleteEntity()
        {
            using (var db = new ApiDb())
            {
                int value = 999999;

                //初次查询没有数据
                var re = db.Queryable <OperateTestModel>().Where(t => t.StringKey.StartsWith("AddTest")).ToList();
                Assert.Null(re);

                //add一条数据
                OperateTestModel model = new OperateTestModel
                {
                    IntKey    = value,
                    StringKey = "AddTest"
                };
                model.IntKey = value;
                db.Add <OperateTestModel>(model);

                //插入后查询有一条记录
                var re1 = db.Queryable <OperateTestModel>().Where(t => t.StringKey.StartsWith("AddTest")).ToList();
                Assert.Single(re1);
                Assert.Equal(value, re1.First().IntKey);

                var entity = db.Queryable <OperateTestModel>().Where(t => t.IntKey == value).ToOne();
                Assert.NotNull(entity);
                Assert.Equal(value, entity.IntKey);

                //删除数据
                db.Delete <OperateTestModel>(entity);

                //删除后查询没有
                var re4 = db.Queryable <OperateTestModel>().Where(t => t.StringKey.StartsWith("AddTest")).ToList();
                Assert.Null(re4);
            }
        }