mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-30 14:32:50 +08:00

- 增加 BaseRepository.AttachOnlyPrimary 方法,只附加实体的主键值; > 在更新前使用可实现不查询数据库再更新、也可以实现更新时不更新值为 null 的字段 ```csharp class T { public int id { get; set; } public string name { get; set; } public string other { get; set; } } var item = new T { id = 1, name = "xx" }; fsql.GetRepository<T>().AttachOnlyPrimary(item).Update(item); //只更新 name ``` - 修复 Lambda 表达式中 DateTime.Now.ToString("yyyyMMdd") 不能直接执行的 bug;
282 lines
9.4 KiB
C#
282 lines
9.4 KiB
C#
using System;
|
||
using Xunit;
|
||
|
||
namespace FreeSql.Tests
|
||
{
|
||
public class RepositoryTests
|
||
{
|
||
|
||
[Fact]
|
||
public void AddUpdate()
|
||
{
|
||
var repos = g.sqlite.GetGuidRepository<AddUpdateInfo>();
|
||
|
||
var item = repos.Insert(new AddUpdateInfo());
|
||
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
|
||
|
||
item = repos.Insert(new AddUpdateInfo { Id = Guid.NewGuid() });
|
||
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
|
||
|
||
item.Title = "xxx";
|
||
repos.Update(item);
|
||
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
|
||
|
||
Console.WriteLine(repos.UpdateDiy.Where(a => a.Id == item.Id).Set(a => a.Clicks + 1).ToSql());
|
||
repos.UpdateDiy.Where(a => a.Id == item.Id).Set(a => a.Clicks + 1).ExecuteAffrows();
|
||
|
||
item = repos.Find(item.Id);
|
||
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
|
||
}
|
||
|
||
[Fact]
|
||
public void UpdateAttach()
|
||
{
|
||
var repos = g.sqlite.GetGuidRepository<AddUpdateInfo>();
|
||
|
||
var item = new AddUpdateInfo { Id = Guid.NewGuid() };
|
||
repos.Attach(item);
|
||
|
||
item.Title = "xxx";
|
||
repos.Update(item); //这行执行 UPDATE "AddUpdateInfo" SET "Title" = 'xxx' WHERE("Id" = '1942fb53-9700-411d-8895-ce4cecdf3257')
|
||
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
|
||
|
||
repos.Update(item); //这行不执行 SQL,未变化
|
||
|
||
repos.AttachOnlyPrimary(item).Update(item); //这行更新状态值,只有主键值存在,执行更新 set title = xxx
|
||
|
||
Console.WriteLine(repos.UpdateDiy.Where(a => a.Id == item.Id).Set(a => a.Clicks + 1).ToSql());
|
||
repos.UpdateDiy.Where(a => a.Id == item.Id).Set(a => a.Clicks + 1).ExecuteAffrows();
|
||
|
||
item = repos.Find(item.Id);
|
||
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
|
||
}
|
||
|
||
[Fact]
|
||
public void UpdateWhenNotExists()
|
||
{
|
||
var repos = g.sqlite.GetGuidRepository<AddUpdateInfo>();
|
||
|
||
var item = new AddUpdateInfo { Id = Guid.NewGuid() };
|
||
item.Title = "xxx";
|
||
Assert.Throws<Exception>(() => repos.Update(item));
|
||
}
|
||
|
||
[Fact]
|
||
public void Update()
|
||
{
|
||
g.sqlite.Insert(new AddUpdateInfo()).ExecuteAffrows();
|
||
|
||
var repos = g.sqlite.GetGuidRepository<AddUpdateInfo>();
|
||
|
||
var item = new AddUpdateInfo { Id = g.sqlite.Select<AddUpdateInfo>().First().Id };
|
||
|
||
item.Title = "xxx";
|
||
repos.Update(item);
|
||
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
|
||
}
|
||
|
||
public class AddUpdateInfo
|
||
{
|
||
|
||
public Guid Id { get; set; }
|
||
public string Title { get; set; }
|
||
|
||
public int Clicks { get; set; } = 10;
|
||
}
|
||
|
||
[Fact]
|
||
public void UnitOfWorkRepository()
|
||
{
|
||
foreach (var fsql in new[] { g.sqlite, /*g.mysql, g.pgsql, g.oracle, g.sqlserver*/ })
|
||
{
|
||
|
||
fsql.CodeFirst.ConfigEntity<FlowModel>(f =>
|
||
{
|
||
f.Property(b => b.UserId).IsPrimary(true);
|
||
f.Property(b => b.Id).IsPrimary(true).IsIdentity(true);
|
||
f.Property(b => b.Name).IsNullable(false);
|
||
});
|
||
|
||
FlowModel flow = new FlowModel()
|
||
{
|
||
CreateTime = DateTime.Now,
|
||
Name = "aaa",
|
||
LastModifyTime = DateTime.Now,
|
||
UserId = 1,
|
||
};
|
||
var flowRepos = fsql.GetRepository<FlowModel>();
|
||
flowRepos.Insert(flow);
|
||
|
||
//事务添加
|
||
flow = new FlowModel()
|
||
{
|
||
CreateTime = DateTime.Now,
|
||
Name = "aaa",
|
||
LastModifyTime = DateTime.Now,
|
||
UserId = 1,
|
||
};
|
||
using (var uow = fsql.CreateUnitOfWork())
|
||
{
|
||
flowRepos = uow.GetRepository<FlowModel>();
|
||
flowRepos.Insert(flow);
|
||
uow.Commit();
|
||
}
|
||
}
|
||
}
|
||
|
||
[Fact]
|
||
public void UnitOfWorkRepositoryWithDisableBeforeInsert()
|
||
{
|
||
foreach (var fsql in new[] { g.sqlite, })
|
||
{
|
||
fsql.CodeFirst.ConfigEntity<FlowModel>(f =>
|
||
{
|
||
f.Property(b => b.UserId).IsPrimary(true);
|
||
f.Property(b => b.Id).IsPrimary(true).IsIdentity(true);
|
||
f.Property(b => b.Name).IsNullable(false);
|
||
});
|
||
|
||
var flowRepos = fsql.GetRepository<FlowModel>();
|
||
|
||
var flow = new FlowModel()
|
||
{
|
||
CreateTime = DateTime.Now,
|
||
Name = "aaa",
|
||
LastModifyTime = DateTime.Now,
|
||
UserId = 1,
|
||
};
|
||
|
||
//清理掉数据库中已存在的数据,为了接下来的插入测试
|
||
flowRepos.Delete(a => a.UserId == 1 && a.Name == "aaa");
|
||
|
||
using (var uow = fsql.CreateUnitOfWork())
|
||
{
|
||
//关闭工作单元(不会开始事务)
|
||
uow.Close();
|
||
var uowFlowRepos = uow.GetRepository<FlowModel>();
|
||
uowFlowRepos.Insert(flow);
|
||
//已关闭工作单元,提不提交都没影响,此处注释来确定工作单元开关是否生效:关闭了,不Commit也应该插入数据
|
||
//uow.Commit();
|
||
}
|
||
|
||
Assert.True(flowRepos.Select.Any(a => a.UserId == 1 && a.Name == "aaa"));
|
||
}
|
||
|
||
}
|
||
|
||
[Fact]
|
||
public void UnitOfWorkRepositoryWithDisableAfterInsert()
|
||
{
|
||
foreach (var fsql in new[] { g.sqlite, })
|
||
{
|
||
fsql.CodeFirst.ConfigEntity<FlowModel>(f =>
|
||
{
|
||
f.Property(b => b.UserId).IsPrimary(true);
|
||
f.Property(b => b.Id).IsPrimary(true).IsIdentity(true);
|
||
f.Property(b => b.Name).IsNullable(false);
|
||
});
|
||
|
||
var flowRepos = fsql.GetRepository<FlowModel>();
|
||
|
||
//清理掉数据库中已存在的数据,为了接下来的插入测试
|
||
flowRepos.Delete(a => a.UserId == 1 && a.Name == "aaa");
|
||
|
||
var flow = new FlowModel()
|
||
{
|
||
CreateTime = DateTime.Now,
|
||
Name = "aaa",
|
||
LastModifyTime = DateTime.Now,
|
||
UserId = 1,
|
||
};
|
||
|
||
|
||
Assert.Throws<Exception>(() =>
|
||
{
|
||
using (var uow = fsql.CreateUnitOfWork())
|
||
{
|
||
var uowFlowRepos = uow.GetRepository<FlowModel>();
|
||
uowFlowRepos.Insert(flow);
|
||
//有了任意 Insert/Update/Delete 调用关闭uow的方法将会发生异常
|
||
uow.Close();
|
||
uow.Commit();
|
||
}
|
||
|
||
});
|
||
}
|
||
}
|
||
|
||
[Fact]
|
||
public void UnitOfWorkRepositoryWithoutDisable()
|
||
{
|
||
foreach (var fsql in new[] { g.sqlite, })
|
||
{
|
||
fsql.CodeFirst.ConfigEntity<FlowModel>(f =>
|
||
{
|
||
f.Property(b => b.UserId).IsPrimary(true);
|
||
f.Property(b => b.Id).IsPrimary(true).IsIdentity(true);
|
||
f.Property(b => b.Name).IsNullable(false);
|
||
});
|
||
|
||
var flowRepos = fsql.GetRepository<FlowModel>();
|
||
if (flowRepos.Select.Any(a => a.UserId == 1 && a.Name == "aaa"))
|
||
{
|
||
flowRepos.Delete(a => a.UserId == 1);
|
||
}
|
||
|
||
|
||
var flow = new FlowModel()
|
||
{
|
||
CreateTime = DateTime.Now,
|
||
Name = "aaa",
|
||
LastModifyTime = DateTime.Now,
|
||
UserId = 1,
|
||
};
|
||
|
||
|
||
using (var uow = fsql.CreateUnitOfWork())
|
||
{
|
||
var uowFlowRepos = uow.GetRepository<FlowModel>();
|
||
uowFlowRepos.Insert(flow);
|
||
//不调用commit将不会提交数据库更改
|
||
//uow.Commit();
|
||
}
|
||
Assert.False(flowRepos.Select.Any(a => a.UserId == 1 && a.Name == "aaa"));
|
||
}
|
||
}
|
||
|
||
|
||
public partial class FlowModel
|
||
{
|
||
public int UserId { get; set; }
|
||
public int Id { get; set; }
|
||
public int? ParentId { get; set; }
|
||
public string Name { get; set; }
|
||
public DateTime CreateTime { get; set; }
|
||
public DateTime LastModifyTime { get; set; }
|
||
public string Desc { get; set; }
|
||
}
|
||
|
||
[Fact]
|
||
public void AsType()
|
||
{
|
||
g.sqlite.Insert(new AddUpdateInfo()).ExecuteAffrows();
|
||
|
||
var repos = g.sqlite.GetGuidRepository<object>();
|
||
repos.AsType(typeof(AddUpdateInfo));
|
||
|
||
var item = new AddUpdateInfo();
|
||
repos.Insert(item);
|
||
repos.Update(item);
|
||
|
||
item.Clicks += 1;
|
||
repos.InsertOrUpdate(item);
|
||
|
||
var item2 = repos.Find(item.Id) as AddUpdateInfo;
|
||
Assert.Equal(item.Clicks, item2.Clicks);
|
||
|
||
repos.DataFilter.Apply("xxx", a => (a as AddUpdateInfo).Clicks == 2);
|
||
Assert.Null(repos.Find(item.Id));
|
||
}
|
||
}
|
||
}
|