#if NET40 using FreeSql.DataAnnotations; using System; #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 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 BaseEntity : BaseEntity where TEntity : class { static BaseEntity() { 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 /// /// Get data based on the value of the primary key
/// 根据主键值获取数据 ///
/// /// public static TEntity Find(TKey id) { var item = Select.WhereDynamic(id).First(); (item as BaseEntity)?.Attach(); return item; } } /// /// Entity base class, including CreateTime/UpdateTime/IsDeleted, and sync/async CRUD methods. /// /// 包括 CreateTime/UpdateTime/IsDeleted、以及 CRUD 异步和同步方法的实体基类 /// /// [Table(DisableSyncStructure = true)] public abstract class BaseEntity : BaseEntityAsync where TEntity : class { bool UpdateIsDeleted(bool value) { if (Repository == null) return Orm.Update(this as TEntity) .WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction()) .Set(a => (a as BaseEntity).IsDeleted, IsDeleted = value) .ExecuteAffrows() == 1; IsDeleted = value; Repository.UnitOfWork = _resolveUow?.Invoke(); return Repository.Update(this as TEntity) == 1; } /// /// To delete data
/// 删除数据 ///
/// To flag whether to delete the physical level of the data /// public virtual bool Delete(bool physicalDelete = false) { if (physicalDelete == false) return UpdateIsDeleted(true); if (Repository == null) return Orm.Delete(this as TEntity) .WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction()) .ExecuteAffrows() == 1; Repository.UnitOfWork = _resolveUow?.Invoke(); return Repository.Delete(this as TEntity) == 1; } /// /// To recover deleted data
/// 恢复删除的数据 ///
/// public virtual bool Restore() => UpdateIsDeleted(false); /// /// To update data
/// 更新数据 ///
/// public virtual bool Update() { UpdateTime = DateTime.Now; if (Repository == null) return Orm.Update() .WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction()) .SetSource(this as TEntity) .ExecuteAffrows() == 1; Repository.UnitOfWork = _resolveUow?.Invoke(); return Repository.Update(this as TEntity) == 1; } /// /// To insert data
/// 插入数据 ///
public virtual TEntity Insert() { CreateTime = DateTime.Now; if (Repository == null) Repository = Orm.GetRepository(); Repository.UnitOfWork = _resolveUow?.Invoke(); return Repository.Insert(this as TEntity); } /// /// To insert or update data
/// 更新或插入 ///
/// public virtual TEntity Save() { UpdateTime = DateTime.Now; if (Repository == null) Repository = Orm.GetRepository(); Repository.UnitOfWork = _resolveUow?.Invoke(); return Repository.InsertOrUpdate(this as TEntity); } /// /// To completely save the navigation properties of the entity in the form of sub-tables.
/// 【完整】保存导航属性,子表 ///
/// Navigation property name public virtual void SaveMany(string navigatePropertyName) { if (Repository == null) Repository = Orm.GetRepository(); Repository.UnitOfWork = _resolveUow?.Invoke(); Repository.SaveMany(this as TEntity, navigatePropertyName); } } }