#if NET40 using FreeSql.DataAnnotations; #else using FreeSql.DataAnnotations; using System; using System.Threading.Tasks; #endif // ReSharper disable once CheckNamespace namespace FreeSql { /// /// 包括 CreateTime/UpdateTime/IsDeleted、CRUD 异步方法、以及 ID 主键定义 的实体基类 /// /// 当 TKey 为 int/long 时,Id 主键被设为自增值主键 /// /// /// [Table(DisableSyncStructure = true)] public abstract class BaseEntityAsync : BaseEntityAsync where TEntity : class { static BaseEntityAsync() { var keyType = typeof(TKey).NullableTypeOrThis(); if (keyType == typeof(int) || keyType == typeof(long)) ConfigEntity(typeof(TEntity), t => t.Property("Id").IsIdentity(true)); } /// /// 主键 /// [Column(Position = 1)] public virtual TKey Id { get; set; } #if !NET40 /// /// 根据主键值获取数据 /// /// /// public static async Task FindAsync(TKey id) { var item = await Select.WhereDynamic(id).FirstAsync(); (item as BaseEntity)?.Attach(); return item; } #endif } /// /// 包括 CreateTime/UpdateTime/IsDeleted、以及 CRUD 异步方法的实体基类 /// /// [Table(DisableSyncStructure = true)] public abstract class BaseEntityAsync : BaseEntityReadOnly where TEntity : class { #if !NET40 async Task UpdateIsDeletedAsync(bool value) { if (Repository is null) { return await Orm.Update(this as TEntity) .WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction()) .Set(a => (a as BaseEntity).IsDeleted, IsDeleted = value).ExecuteAffrowsAsync() == 1; } IsDeleted = value; Repository.UnitOfWork = _resolveUow?.Invoke(); return await Repository.UpdateAsync(this as TEntity) == 1; } /// /// 删除数据 /// /// 是否物理删除 /// public virtual async Task DeleteAsync(bool physicalDelete = false) { if (physicalDelete == false) return await UpdateIsDeletedAsync(true); if (Repository is null) return await Orm.Delete(this as TEntity).ExecuteAffrowsAsync() == 1; Repository.UnitOfWork = _resolveUow?.Invoke(); return await Repository.DeleteAsync(this as TEntity) == 1; } /// /// 恢复删除的数据 /// /// public virtual Task RestoreAsync() => UpdateIsDeletedAsync(false); /// /// 更新数据 /// /// public virtual async Task UpdateAsync() { UpdateTime = DateTime.Now; if (Repository is null) { return await Orm.Update() .WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction()) .SetSource(this as TEntity).ExecuteAffrowsAsync() == 1; } Repository.UnitOfWork = _resolveUow?.Invoke(); return await Repository.UpdateAsync(this as TEntity) == 1; } /// /// 插入数据 /// public virtual Task InsertAsync() { CreateTime = DateTime.Now; if (Repository is null) Repository = Orm.GetRepository(); Repository.UnitOfWork = _resolveUow?.Invoke(); return Repository.InsertAsync(this as TEntity); } /// /// 更新或插入 /// /// public virtual Task SaveAsync() { UpdateTime = DateTime.Now; if (Repository is null) Repository = Orm.GetRepository(); Repository.UnitOfWork = _resolveUow?.Invoke(); return Repository.InsertOrUpdateAsync(this as TEntity); } /// /// 【完整】保存导航属性,子表 /// /// 导航属性名 public virtual Task SaveManyAsync(string navigatePropertyName) { if (Repository is null) Repository = Orm.GetRepository(); Repository.UnitOfWork = _resolveUow?.Invoke(); return Repository.SaveManyAsync(this as TEntity, navigatePropertyName); } #endif } }