using FreeSql;
using FreeSql.DataAnnotations;
using System;
using System.Data;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
///
/// 包括 CreateTime/UpdateTime/IsDeleted、CRUD 方法、以及 ID 主键定义 的实体基类
///
/// 当 TKey 为 int/long 时,Id 主键被设为自增值主键
///
///
///
[Table(DisableSyncStructure = true)]
public abstract class BaseEntity : BaseEntity 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));
}
///
/// 主键
///
public virtual TKey Id { get; set; }
///
/// 根据主键值获取数据
///
///
///
async public static Task FindAsync(TKey id)
{
var item = await Select.WhereDynamic(id).FirstAsync();
(item as BaseEntity)?.Attach();
return item;
}
///
/// 根据主键值获取数据
///
///
///
public static TEntity Find(TKey id)
{
var item = Select.WhereDynamic(id).First();
(item as BaseEntity)?.Attach();
return item;
}
}