using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CloudBuilder.Core.DatabaseAccessor.Entity { /// /// 实体类基类 /// public abstract class EntityBase : IEntity { /// /// 获取或设置 编号 /// [DisplayName("编号")] [NotMapped] public TKey? Id { get; set; } public virtual string GetSelectFrom() { return null; } public virtual string GetSelectCountFrom(string sqlWhere) { return null; } public virtual string GetSelectTopFrom(int limit, int start, string orderby, string orderbytag, string sqlWhere) { return null; } /// /// 判断两个实体是否是同一数据记录的实体 /// /// 要比较的实体信息 /// public override bool Equals(object obj) { if (obj == null) { return false; } if (!(obj is EntityBase entity)) { return false; } return IsKeyEqual(entity.Id, Id); } /// /// 实体ID是否相等 /// public static bool IsKeyEqual(TKey id1, TKey id2) { if (id1 == null && id2 == null) { return true; } if (id1 == null || id2 == null) { return false; } return Equals(id1, id2); } /// /// 用作特定类型的哈希函数。 /// /// /// 当前 的哈希代码。
/// 如果Idnull则返回0, /// 如果不为null则返回Id对应的哈希值 ///
public override int GetHashCode() { if (Id == null) { return 0; } return Id.ToString().GetHashCode(); } } }