#if NET40 using FreeSql.DataAnnotations; #else using FreeSql.DataAnnotations; using System; using System.Threading.Tasks; #endif // ReSharper disable once CheckNamespace namespace FreeSql { /// /// Entity base class, including CreateTime/UpdateTime/IsDeleted, the async CRUD methods, and ID primary key definition. /// /// 包括 CreateTime/UpdateTime/IsDeleted、CRUD 异步方法、以及 ID 主键定义 的实体基类 /// /// When TKey is int/long, the Id is set to be an auto-incremented primary key /// /// 当 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)); } /// /// Primary key
/// 主键 ///
[Column(Position = 1)] public virtual TKey Id { get; set; } #if !NET40 /// /// Get data based on the value of the primary key
/// 根据主键值获取数据 ///
/// /// public static async Task FindAsync(TKey id) { var item = await Select.WhereDynamic(id).FirstAsync(); (item as BaseEntity)?.Attach(); return item; } #endif } /// /// Entity base class, including CreateTime/UpdateTime/IsDeleted, and async CRUD methods. /// /// 包括 CreateTime/UpdateTime/IsDeleted、以及 CRUD 异步方法的实体基类 /// /// [Table(DisableSyncStructure = true)] public abstract class BaseEntityAsync : BaseEntityReadOnly where TEntity : class { #if !NET40 async Task UpdateIsDeletedAsync(bool value) { if (Repository == 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; } /// /// To delete data
/// 删除数据 ///
/// To flag whether to delete the physical level of the data /// public virtual async Task DeleteAsync(bool physicalDelete = false) { if (physicalDelete == false) return await UpdateIsDeletedAsync(true); if (Repository == null) return await Orm.Delete(this as TEntity) .WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction()) .ExecuteAffrowsAsync() == 1; Repository.UnitOfWork = _resolveUow?.Invoke(); return await Repository.DeleteAsync(this as TEntity) == 1; } /// /// To recover deleted data
/// 恢复删除的数据 ///
/// public virtual Task RestoreAsync() => UpdateIsDeletedAsync(false); /// /// To update data
/// 更新数据 ///
/// public virtual async Task UpdateAsync() { UpdateTime = DateTime.Now; if (Repository == 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; } /// /// To insert data
/// 插入数据 ///
public virtual Task InsertAsync() { CreateTime = DateTime.Now; if (Repository == null) Repository = Orm.GetRepository(); Repository.UnitOfWork = _resolveUow?.Invoke(); return Repository.InsertAsync(this as TEntity); } /// /// To insert or update data
/// 更新或插入 ///
/// public virtual Task SaveAsync() { UpdateTime = DateTime.Now; if (Repository == null) Repository = Orm.GetRepository(); Repository.UnitOfWork = _resolveUow?.Invoke(); return Repository.InsertOrUpdateAsync(this as TEntity); } /// /// To completely save the navigation properties of the entity in the form of sub-tables.
/// 【完整】保存导航属性,子表 ///
/// Navigation property name public virtual Task SaveManyAsync(string navigatePropertyName) { if (Repository == null) Repository = Orm.GetRepository(); Repository.UnitOfWork = _resolveUow?.Invoke(); return Repository.SaveManyAsync(this as TEntity, navigatePropertyName); } #endif } }