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); } }
public void Add() { using (var db = new ApiDb()) { db.Add(new OperationTest { Id = 1, Key2 = 1, IntKey = 1, StringKey = "1", IntNullKey = null, FloatNullKey = 1, DoubleNullKey = 1, DateNullKey = DateTime.MinValue, DateTimeNullKey = null }); Assert.Equal("INSERT INTO OperateTest (Key2,StringKey,IntKey,IntNullKey,DateNullKey,DateTimeNullKey,DoubleNullKey) VALUES (@Key2,@StringKey,@IntKey,@IntNullKey,@DateNullKey,@DateTimeNullKey,@DoubleNullKey)", db.SqlStatement); Assert.Equal(new[] { "@Key2", "@StringKey", "@IntKey", "@IntNullKey", "@DateNullKey", "@DateTimeNullKey", "@DoubleNullKey" }, 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); } }