mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 04:18:16 +08:00
- 增加 ISelect.WhereCascade 实现多表查询时,向每个表中附加条件;
- 增加 Examples 项目 base_entity,利用 BaseEntity 实现简洁的数据库操作;
This commit is contained in:
163
Examples/base_entity/BaseEntity.cs
Normal file
163
Examples/base_entity/BaseEntity.cs
Normal file
@ -0,0 +1,163 @@
|
||||
using FreeSql;
|
||||
using FreeSql.DataAnnotations;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
[Table(DisableSyncStructure = true)]
|
||||
public abstract class BaseEntity
|
||||
{
|
||||
private static Lazy<IFreeSql> _ormLazy = new Lazy<IFreeSql>(() =>
|
||||
{
|
||||
var orm = new FreeSqlBuilder()
|
||||
.UseAutoSyncStructure(true)
|
||||
.UseNoneCommandParameter(true)
|
||||
.UseConnectionString(DataType.Sqlite, "data source=test.db;max pool size=5")
|
||||
.Build();
|
||||
orm.Aop.CurdBefore += (s, e) => Trace.WriteLine(e.Sql + "\r\n");
|
||||
return orm;
|
||||
});
|
||||
public static IFreeSql Orm => _ormLazy.Value;
|
||||
|
||||
/// <summary>
|
||||
/// 创建时间
|
||||
/// </summary>
|
||||
public DateTime CreateTime { get; set; }
|
||||
/// <summary>
|
||||
/// 更新时间
|
||||
/// </summary>
|
||||
public DateTime UpdateTime { get; set; }
|
||||
/// <summary>
|
||||
/// 逻辑删除
|
||||
/// </summary>
|
||||
public bool IsDeleted { get; set; }
|
||||
}
|
||||
|
||||
[Table(DisableSyncStructure = true)]
|
||||
public abstract class BaseEntity<TEntity> : BaseEntity where TEntity : class
|
||||
{
|
||||
public static ISelect<TEntity> Select => Orm.Select<TEntity>().WhereCascade(a => (a as BaseEntity<TEntity>).IsDeleted == false);
|
||||
public static ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => Select.Where(exp);
|
||||
public static ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => Select.WhereIf(condition, exp);
|
||||
|
||||
[JsonIgnore]
|
||||
protected IBaseRepository<TEntity> Repository { get; set; }
|
||||
|
||||
bool UpdateIsDeleted(bool value)
|
||||
{
|
||||
if (this.Repository == null)
|
||||
return Orm.Update<TEntity>(this as TEntity).Set(a => (a as BaseEntity<TEntity>).IsDeleted, this.IsDeleted = value).ExecuteAffrows() == 1;
|
||||
this.IsDeleted = value;
|
||||
return this.Repository.Update(this as TEntity) == 1;
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual bool Delete() => this.UpdateIsDeleted(true);
|
||||
/// <summary>
|
||||
/// 恢复删除的数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual bool Restore() => this.UpdateIsDeleted(false);
|
||||
|
||||
/// <summary>
|
||||
/// 附加实体,在更新数据时,只更新变化的部分
|
||||
/// </summary>
|
||||
public void Attach()
|
||||
{
|
||||
if (this.Repository == null) this.Repository = Orm.GetRepository<TEntity>();
|
||||
this.Repository.Attach(this as TEntity);
|
||||
}
|
||||
/// <summary>
|
||||
/// 更新数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual bool Update()
|
||||
{
|
||||
if (this.Repository == null)
|
||||
return Orm.Update<TEntity>().SetSource(this as TEntity).ExecuteAffrows() == 1;
|
||||
return this.Repository.Update(this as TEntity) == 1;
|
||||
}
|
||||
/// <summary>
|
||||
/// 插入数据
|
||||
/// </summary>
|
||||
public virtual void Insert()
|
||||
{
|
||||
if (this.Repository == null) this.Repository = Orm.GetRepository<TEntity>();
|
||||
this.Repository.Insert(this as TEntity);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新或插入
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual void Save()
|
||||
{
|
||||
if (this.Repository == null) this.Repository = Orm.GetRepository<TEntity>();
|
||||
this.Repository.InsertOrUpdate(this as TEntity);
|
||||
}
|
||||
}
|
||||
|
||||
[Table(DisableSyncStructure = true)]
|
||||
public abstract class BaseEntity<TEntity, TKey> : BaseEntity<TEntity> where TEntity : class
|
||||
{
|
||||
static BaseEntity()
|
||||
{
|
||||
var tkeyType = typeof(TKey)?.NullableTypeOrThis();
|
||||
if (tkeyType == typeof(int) || tkeyType == typeof(long))
|
||||
Orm.CodeFirst.ConfigEntity(typeof(TEntity),
|
||||
t => t.Property("Id").IsIdentity(true));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 主键
|
||||
/// </summary>
|
||||
public virtual TKey Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 根据主键值获取数据
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public static TEntity Find(TKey id)
|
||||
{
|
||||
var item = Select.WhereDynamic(id).First();
|
||||
(item as BaseEntity<TEntity>)?.Attach();
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
[Table(DisableSyncStructure = true)]
|
||||
public abstract class BaseEntity<TEntity, TKey1, TKey2> : BaseEntity<TEntity> where TEntity : class
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 主键1
|
||||
/// </summary>
|
||||
[Column(IsPrimary = true)]
|
||||
public virtual TKey1 PkId1 { get; set; }
|
||||
/// <summary>
|
||||
/// 主键2
|
||||
/// </summary>
|
||||
[Column(IsPrimary = true)]
|
||||
public virtual TKey2 PkId2 { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 根据主键值获取数据
|
||||
/// </summary>
|
||||
/// <param name="pkid1">主键1</param>
|
||||
/// <param name="pkid2">主键2</param>
|
||||
/// <returns></returns>
|
||||
public static TEntity Find(TKey1 pkid1, TKey1 pkid2)
|
||||
{
|
||||
var item = Select.WhereDynamic(new
|
||||
{
|
||||
PkId1 = pkid1,
|
||||
PkId2 = pkid2
|
||||
}).First();
|
||||
(item as BaseEntity<TEntity>).Attach();
|
||||
return item;
|
||||
}
|
||||
}
|
91
Examples/base_entity/Entities/User.cs
Normal file
91
Examples/base_entity/Entities/User.cs
Normal file
@ -0,0 +1,91 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
public class UserGroup : BaseEntity<UserGroup, int>
|
||||
{
|
||||
/// <summary>
|
||||
/// 组名
|
||||
/// </summary>
|
||||
public string GroupName { get; set; }
|
||||
|
||||
public List<User1> User1s { get; set; }
|
||||
|
||||
public List<User2> User2s { get; set; }
|
||||
}
|
||||
|
||||
public class Role : BaseEntity<Role, string>
|
||||
{
|
||||
public List<User1> User1s { get; set; }
|
||||
}
|
||||
public class RoleUser1 : BaseEntity<RoleUser1>
|
||||
{
|
||||
public string RoleId { get; set; }
|
||||
public Guid User1Id { get; set; }
|
||||
|
||||
public Role Role { get; set; }
|
||||
public User1 User1 { get; set; }
|
||||
}
|
||||
|
||||
public class User1 : BaseEntity<User1, Guid>
|
||||
{
|
||||
public int GroupId { get; set; }
|
||||
public UserGroup Group { get; set; }
|
||||
|
||||
public List<Role> Roles { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 登陆名
|
||||
/// </summary>
|
||||
public string Username { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 昵称
|
||||
/// </summary>
|
||||
public string Nickname { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 头像
|
||||
/// </summary>
|
||||
public string Avatar { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 描述
|
||||
/// </summary>
|
||||
public string Description { get; set; }
|
||||
}
|
||||
|
||||
public class User2 : BaseEntity<User2, Guid, int>
|
||||
{
|
||||
static User2()
|
||||
{
|
||||
User2.Orm.CodeFirst.ConfigEntity<User2>(t =>
|
||||
{
|
||||
t.Property(a => a.PkId1).Name("UserId");
|
||||
t.Property(a => a.PkId2).Name("Index");
|
||||
});
|
||||
}
|
||||
|
||||
public int GroupId { get; set; }
|
||||
public UserGroup Group { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 登陆名
|
||||
/// </summary>
|
||||
public string Username { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 昵称
|
||||
/// </summary>
|
||||
public string Nickname { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 头像
|
||||
/// </summary>
|
||||
public string Avatar { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 描述
|
||||
/// </summary>
|
||||
public string Description { get; set; }
|
||||
}
|
82
Examples/base_entity/Program.cs
Normal file
82
Examples/base_entity/Program.cs
Normal file
@ -0,0 +1,82 @@
|
||||
using System;
|
||||
|
||||
namespace base_entity
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var ug1 = new UserGroup();
|
||||
ug1.GroupName = "分组一";
|
||||
ug1.Insert();
|
||||
|
||||
var ug2 = new UserGroup();
|
||||
ug2.GroupName = "分组二";
|
||||
ug2.Insert();
|
||||
|
||||
var u1 = new User1();
|
||||
var u2 = new User2();
|
||||
|
||||
u1.GroupId = ug1.Id;
|
||||
u1.Save();
|
||||
|
||||
u2.GroupId = ug2.Id;
|
||||
u2.Save();
|
||||
|
||||
u1.Delete();
|
||||
u1.Restore();
|
||||
|
||||
u1.Nickname = "x1";
|
||||
u1.Update();
|
||||
|
||||
u2.Delete();
|
||||
u2.Restore();
|
||||
|
||||
u2.Username = "x2";
|
||||
u2.Update();
|
||||
|
||||
var u11 = User1.Find(u1.Id);
|
||||
u11.Description = "备注";
|
||||
u11.Save();
|
||||
|
||||
u11.Delete();
|
||||
|
||||
var u11null = User1.Find(u1.Id);
|
||||
|
||||
var u11s = User1.Where(a => a.Group.Id == ug1.Id).Limit(10).ToList();
|
||||
var u22s = User2.Where(a => a.Group.Id == ug2.Id).Limit(10).ToList();
|
||||
|
||||
var u11s2 = User1.Select.LeftJoin<UserGroup>((a, b) => a.GroupId == b.Id).Limit(10).ToList();
|
||||
var u22s2 = User2.Select.LeftJoin<UserGroup>((a, b) => a.GroupId == b.Id).Limit(10).ToList();
|
||||
|
||||
var ug1s = UserGroup.Select
|
||||
.IncludeMany(a => a.User1s)
|
||||
.IncludeMany(a => a.User2s)
|
||||
.Limit(10).ToList();
|
||||
|
||||
var ug1s2 = UserGroup.Select.Where(a => a.User1s.AsSelect().Any(b => b.Nickname == "x1")).Limit(10).ToList();
|
||||
|
||||
var r1 = new Role();
|
||||
r1.Id = "管理员";
|
||||
r1.Save();
|
||||
|
||||
var r2 = new Role();
|
||||
r2.Id = "超级会员";
|
||||
r2.Save();
|
||||
|
||||
var ru1 = new RoleUser1();
|
||||
ru1.User1Id = u1.Id;
|
||||
ru1.RoleId = r1.Id;
|
||||
ru1.Save();
|
||||
|
||||
ru1.RoleId = r2.Id;
|
||||
ru1.Save();
|
||||
|
||||
var u1roles = User1.Select.IncludeMany(a => a.Roles).ToList();
|
||||
var u1roles2 = User1.Select.Where(a => a.Roles.AsSelect().Any(b => b.Id == "xx")).ToList();
|
||||
|
||||
Console.WriteLine("按任意键结束。。。");
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
}
|
18
Examples/base_entity/base_entity.csproj
Normal file
18
Examples/base_entity/base_entity.csproj
Normal file
@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\FreeSql.Repository\FreeSql.Repository.csproj" />
|
||||
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
|
||||
<ProjectReference Include="..\..\Providers\FreeSql.Provider.Sqlite\FreeSql.Provider.Sqlite.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Reference in New Issue
Block a user