Skip to content
Promote Your Product

Entity Base Classes

Namespace: ZhonTai.Admin.Core.Entities

Zhongtai Admin provides a complete entity base class system that implements common functionality such as primary keys, audit fields, soft delete, multi-tenancy, data permissions, and optimistic locking through interface composition and inheritance chains.

Use Cases

  • Choose the appropriate base class when creating database table entities
  • Avoid repeating common fields in every entity
  • Automatically benefit from global filters (soft delete, tenant isolation, data permissions)

Inheritance Hierarchy

Primary Key Entity Chain (with audit + soft delete)

Entity<TKey>                        Primary key Id (Snowflake)
  └── EntityAdd<TKey>               + Create audit fields
        └── EntityUpdate<TKey>      + Update audit fields
              └── EntityDelete<TKey> + Soft delete field
                    └── EntityBase    Complete base class (recommended)

No-PK Entity Chain (audit only)

IEntityAdd                          Create audit interface
  └── EntityAddNoId                 + Create audit fields
        └── EntityUpdateNoId        + Update audit fields

EntityBase<TKey> Derived Classes

EntityBase<TKey>
  ├── EntityTenant<TKey>            + Tenant isolation
  │     └── EntityTenantWithData<TKey> + Data permissions
  ├── EntityData<TKey>              + Data permissions (no tenant)
  ├── EntityVersion<TKey>           + Optimistic locking
  └── EntityMember<TKey>            + Member (with soft delete + time audit)
        └── EntityMemberWithTenant<TKey> + Tenant isolation

Composable Interfaces

InterfaceFieldsDescription
IEntity<TKey>IdPrimary key
IEntityAddCreatedUserId/Name/RealName/TimeCreate audit
IEntityUpdateModifiedUserId/Name/RealName/TimeUpdate audit
IDeleteIsDeletedSoft delete
ITenantTenantIdMulti-tenancy
IMemberMemberIdMember
IDataOwnerId/OwnerOrgId/OwnerOrgNameData permissions
IVersionVersionOptimistic locking
IChilds<T>List<T> ChildsTree children

Base Class Fields

Entity<TKey>

Base entity with Snowflake ID primary key.

csharp
public class Entity<TKey> : IEntity<TKey>
{
    [Snowflake] // Auto-generated Snowflake ID
    public virtual TKey Id { get; set; }
}

Non-generic shortcut: Entity (i.e. Entity<long>)

TIP

Typically use the non-generic shortcut class Entity (i.e. Entity<long>, primary key is long). No need to specify a generic parameter.

EntityAdd<TKey>

Adds create audit fields to Entity:

FieldTypeDescription
CreatedUserIdlong?Creator user ID
CreatedUserNamestringCreator username
CreatedUserRealNamestringCreator real name
CreatedTimeDateTime?Created time ([ServerTime] auto-assigned)

Non-generic shortcut: EntityAdd (i.e. EntityAdd<long>)

EntityUpdate<TKey>

Adds update audit fields to EntityAdd:

FieldTypeDescription
ModifiedUserIdlong?Modifier user ID
ModifiedUserNamestringModifier username
ModifiedUserRealNamestringModifier real name
ModifiedTimeDateTime?Modified time (CanInsert=false, CanUpdate=true)

Non-generic shortcut: EntityUpdate (i.e. EntityUpdate<long>)

EntityDelete<TKey>

Adds soft delete to EntityUpdate:

FieldTypeDefaultDescription
IsDeletedboolfalseSoft delete flag

Non-generic shortcut: EntityDelete (i.e. EntityDelete<long>)

TIP

Entities inheriting IDelete automatically convert delete operations to IsDeleted=true updates. FreeSql global filters automatically filter deleted records.

EntityBase<TKey>

Complete base class inheriting EntityDelete (includes primary key + audit + soft delete). The most commonly used base class.

Non-generic shortcut: EntityBase (i.e. EntityBase<long>)

EntityAddNoId

No-primary-key create audit entity. Implements IEntityAdd only. Suitable for tables without independent primary key (e.g. association tables, log tables).

FieldTypeDescription
CreatedUserIdlong?Creator user ID
CreatedUserNamestringCreator username
CreatedUserRealNamestringCreator real name
CreatedTimeDateTime?Created time ([ServerTime] auto-assigned)

EntityUpdateNoId

No-primary-key update audit entity. Inherits EntityAddNoId, implements IEntityUpdate.

FieldTypeDescription
ModifiedUserIdlong?Modifier user ID
ModifiedUserNamestringModifier username
ModifiedUserRealNamestringModifier real name
ModifiedTimeDateTime?Modified time (CanInsert=false, CanUpdate=true)

EntityTenant<TKey>

Multi-tenant entity. Inherits EntityBase, implements ITenant. When multi-tenancy is enabled, queries are automatically filtered by tenant — each tenant can only access its own data.

FieldTypeDescription
TenantIdlong?Tenant ID (cannot be modified after creation)

Non-generic shortcut: EntityTenant (i.e. EntityTenant<long>)

EntityTenantWithData<TKey>

Multi-tenant + data permission entity. Inherits EntityTenant, implements IData. Supports both tenant isolation and data permission filtering.

FieldTypeDescription
TenantIdlong?Tenant ID (from EntityTenant)
OwnerIdlong?Owner ID
OwnerOrgIdlong?Owner department ID
OwnerOrgNamestringOwner department name

Non-generic shortcut: EntityTenantWithData (i.e. EntityTenantWithData<long>)

EntityData<TKey>

Data permission entity. Inherits EntityBase, implements IData. Supports data permission filtering (without tenant isolation). Suitable for scenarios that need data permissions but not multi-tenancy.

FieldTypeDescription
OwnerIdlong?Owner ID
OwnerOrgIdlong?Owner department ID
OwnerOrgNamestringOwner department name

Non-generic shortcut: EntityData (i.e. EntityData<long>)

EntityVersion<TKey>

Optimistic locking entity. Inherits EntityBase, implements IVersion. Updates automatically check the version number. A concurrency exception is thrown if versions don't match. Suitable for high-concurrency update scenarios.

FieldTypeDescription
VersionlongData version ([Column(IsVersion = true)])

Non-generic shortcut: EntityVersion (i.e. EntityVersion<long>)

EntityMember<TKey>

Member entity. Inherits Entity, implements IMember and IDelete. Unlike EntityBase, it does not include full audit fields — only MemberId, time audit, and soft delete.

FieldTypeDescription
IdTKeyPrimary key (Snowflake ID)
MemberIdlong?Member ID
CreatedTimeDateTime?Created time ([ServerTime])
ModifiedTimeDateTime?Modified time (CanInsert=false, CanUpdate=true)
IsDeletedboolSoft delete flag (default false)

Non-generic shortcut: EntityMember (i.e. EntityMember<long>)

EntityMemberWithTenant<TKey>

Member + tenant entity. Inherits EntityMember, implements ITenant.

FieldTypeDescription
MemberIdlong?Member ID (from EntityMember)
TenantIdlong?Tenant ID (cannot be modified after creation)

Non-generic shortcut: EntityMemberWithTenant (i.e. EntityMemberWithTenant<long>)

Examples

Basic Entity (with audit + soft delete)

The most commonly used entity. Inherits EntityBase, includes primary key, create/update audit, and soft delete.

csharp
using FreeSql.DataAnnotations;
using ZhonTai.Admin.Core.Entities;

public class ArticleEntity : EntityBase
{
    [Description("Title")]
    [MaxLength(200)]
    public string Title { get; set; }

    [Description("Content")]
    [Column(StringLength = -1)]
    public string Content { get; set; }

    public bool Enabled { get; set; }
}

Multi-tenant Entity

Use when tenant data isolation is needed. Inherits EntityTenant.

csharp
public class ConfigEntity : EntityTenant
{
    [Description("Key")]
    [MaxLength(100)]
    public string Key { get; set; }

    [Description("Value")]
    [Column(StringLength = -1)]
    public string Value { get; set; }
}

Multi-tenant + Data Permission Entity

Use when both tenant isolation and data permission filtering are needed. Inherits EntityTenantWithData.

csharp
public class OrderEntity : EntityTenantWithData
{
    public string OrderNo { get; set; }
    public decimal Amount { get; set; }
}

This is equivalent to having all these fields: Id, audit fields, IsDeleted, TenantId, OwnerId, OwnerOrgId, OwnerOrgName.

Data Permission Entity (no tenant)

Use when data permissions are needed without multi-tenancy. Inherits EntityData.

csharp
public class ProjectEntity : EntityData
{
    public string Name { get; set; }
    public long? OwnerId { get; set; }               // Owner
    public long? OwnerOrgId { get; set; }            // Department
}

Optimistic Locking Entity

Use in high-concurrency update scenarios to prevent conflicts. Inherits EntityVersion.

csharp
public class SettingEntity : EntityVersion
{
    [Description("Key")]
    [MaxLength(100)]
    public string Key { get; set; }

    [Description("Value")]
    [Column(StringLength = -1)]
    public string Value { get; set; }
}

Member Entity

For frontend member data. Inherits EntityMember, includes MemberId, time audit, and soft delete.

csharp
public class AddressEntity : EntityMember
{
    [Description("Receiver")]
    [MaxLength(50)]
    public string Receiver { get; set; }

    [Description("Phone")]
    [MaxLength(20)]
    public string Phone { get; set; }

    [Description("Address")]
    [MaxLength(200)]
    public string Address { get; set; }
}

Member + Tenant Entity

Use when member data needs tenant isolation. Inherits EntityMemberWithTenant.

csharp
public class CartEntity : EntityMemberWithTenant
{
    public long ProductId { get; set; }
    public int Quantity { get; set; }
}

No-PK Entity (association table)

For tables without independent primary key. Inherits EntityUpdateNoId.

csharp
public class ArticleTagEntity : EntityUpdateNoId
{
    public long ArticleId { get; set; }
    public long TagId { get; set; }
}

Tree Entity

For tree structures. Inherits EntityBase and implements IChilds<T>.

csharp
public class CategoryEntity : EntityBase, IChilds<CategoryEntity>
{
    [Description("Parent ID")]
    public long ParentId { get; set; }

    [Description("Name")]
    [MaxLength(50)]
    public string Name { get; set; }

    public List<CategoryEntity> Childs { get; set; }
}

Tree + Multi-tenant Entity

csharp
public class CategoryEntity : EntityTenant, IChilds<CategoryEntity>
{
    public long ParentId { get; set; }
    public string Name { get; set; }
    public List<CategoryEntity> Childs { get; set; }
}

Primary Key Only Entity

When only a Snowflake ID is needed without audit and soft delete. Inherits Entity.

csharp
public class DictEntity : Entity
{
    public string Name { get; set; }
    public string Value { get; set; }
}

Auto-assignment Attributes

AttributeDescription
[Snowflake]Auto-generate Snowflake ID for primary key
[ServerTime]Auto-assign server time on insert
[ServerTime(CanInsert=false, CanUpdate=true)]Auto-assign server time on update only
[Column(IsVersion = true)]Optimistic locking version field