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.
4 Answer(s)
-
0
- In ABP,
CreatorIdandCreationTimeare intentionally not publicly settable onCreationAuditedEntity<TKey>/AuditedEntity<TKey>(their setters areprotected 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:
Preferred: Don’t set them at all (let ABP set them)
- When you insert via repository / DbContext within a Unit Of Work and
CurrentUseris available, ABP will automatically set:CreationTime(fromIClock)CreatorId(fromICurrentUser.Id)
- When you insert via repository / DbContext within a Unit Of Work and
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 setis accessible from the derived type, but still prevents random code from changing it.
- If you must set from outside: use ABP’s reflection helper
- ABP support commonly recommends
ObjectHelper.TrySetProperty(...)(uses reflection internally) for such cases:
- ABP support commonly recommends
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) - In ABP,
-
0
Hi,
You're right — the setters on
CreationAuditedAggregateRoot<TKey>(inherited byAuditedAggregateRoot<TKey>) are public, while the ones onCreationAuditedEntity<TKey>areprotected. So onAuditedEntity<TKey>you can't assignCreatorId/CreationTimefrom outside the entity.For your case, use
ObjectHelper.TrySetProperty— it's the same helper ABP's ownAuditPropertySetteruses 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/CreatorIdwhen they're still at their default value (default(DateTime)andnull). So if you set them yourself beforeSaveChangesAsync, 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)