Open Closed

AuditedEntity - Set CreatorId and CreationTime #10726


User avatar
0
kkmy created

Hello,

How can I set CreatorId and CreationTime for an entity that is of type AuditedEntity? When I try to get the entity and set it directly like below, I get this error:

entity.CreatorId = user.Id; The property 'Volo.Abp.Domain.Entities.Auditing.CreationAuditedEntity<TKey>.CreatorId' cannot be used in this context because the 'set' accessor is inaccessible

This is not the case for AuditedAggregateRoot. I can do the exact same thing without errors.

What should I do to set those properties?

Thank you.

Markdown supported.
Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)

4 Answer(s)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent
    • In ABP, CreatorId and CreationTime are intentionally not publicly settable on CreationAuditedEntity<TKey> / AuditedEntity<TKey> (their setters are protected set). This is to prevent accidentally changing “creation audit” values during updates; ABP sets them automatically when inserting an entity.
    • So, you have 3 correct options depending on your scenario:
    1. Preferred: Don’t set them at all (let ABP set them)

      • When you insert via repository / DbContext within a Unit Of Work and CurrentUser is available, ABP will automatically set:
        • CreationTime (from IClock)
        • CreatorId (from ICurrentUser.Id)
    2. Set them inside your entity class (because setters are protected)

      • Add a domain method (or set them in the constructor) inside your derived entity:
    public class MyEntity : AuditedEntity<Guid>
       {
           // ...
    
           public void SetCreationAudit(Guid? creatorId, DateTime creationTime)
           {
               CreatorId = creatorId;
               CreationTime = creationTime;
           }
       }
    
    • This works because protected set is accessible from the derived type, but still prevents random code from changing it.
    1. If you must set from outside: use ABP’s reflection helper
      • ABP support commonly recommends ObjectHelper.TrySetProperty(...) (uses reflection internally) for such cases:
    ObjectHelper.TrySetProperty(entity, x => x.CreatorId, () => CurrentUser.Id);
       ObjectHelper.TrySetProperty(entity, x => x.CreationTime, () => Clock.Now);
    
    • This is useful for edge cases (imports, background jobs, distributed event handlers, etc.) where you really need to override the default auditing behavior.

    Sources:

    • https://abp.io/docs/latest/release-info/migration-guides/abp-4-0
    • https://abp.io/docs/latest/framework/architecture/domain-driven-design/entities
    • https://abp.io/support/questions/6096

    This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Hi,

    You're right — the setters on CreationAuditedAggregateRoot<TKey> (inherited by AuditedAggregateRoot<TKey>) are public, while the ones on CreationAuditedEntity<TKey> are protected. So on AuditedEntity<TKey> you can't assign CreatorId / CreationTime from outside the entity.

    For your case, use ObjectHelper.TrySetProperty — it's the same helper ABP's own AuditPropertySetter uses internally for these fields, so it works through reflection on the protected setters:

    using Volo.Abp;
    
    ObjectHelper.TrySetProperty(entity, x => x.CreatorId, () => user.Id);
    ObjectHelper.TrySetProperty(entity, x => x.CreationTime, () => Clock.Now);
    

    One thing worth knowing: ABP's auto-auditing only fills CreationTime / CreatorId when they're still at their default value (default(DateTime) and null). So if you set them yourself before SaveChangesAsync, ABP won't overwrite them — handy for data imports or creating records on behalf of another user.

    Thanks

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    kkmy created

    My case is exactly what you have described i.e. trying to import from an external project.

    This works brilliantly, thanks maliming!

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Great 👍

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.8.0-preview. Updated on September 16, 2026, 14:50
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.