Hi,
How to rename base column in FullAuditedEntity like this: CreatorUserId -> InputUN CreationTime -> InputTime
please help..
10 Answer(s)
-
0
Override and use [ColumnName] attribute:
public class MyFullAuditedEntity : FullAuditedEntity { [ColumnName("InputUN")] public override long? CreatorUserId { get; set; } [ColumnName("InputTime")] public override DateTime CreationTime { get; set; } }
-
0
Hi @aaron,
Thanks for your help, appreciate it!
How to set value LastModificationTime and LastModifierUserId when Repository.Insert executed?
-
0
That's not a common behavior, but you might be able to achieve that by doing:
public virtual void MyMethod() { //... _repository.Insert(entity); _unitOfWorkManager.Current.SaveChanges(); // Tracking state is EntityState.Added entity.LastModificationTime = entity.CreationTime; // Change tracking state to EntityState.Modified; will be overwritten _unitOfWorkManager.Current.SaveChanges(); // Check that entity.LastModifierUserId is set // ... }
If you want this for all entities, override ApplyAbpConceptsForAddedEntity in MyDbContext:
protected override void ApplyAbpConceptsForAddedEntity(EntityEntry entry, long? userId, EntityChangeReport changeReport) { base.ApplyAbpConceptsForAddedEntity(entry, userId, changeReport); SetModificationAuditProperties(entry.Entity, userId); }
-
0
Hi @aaron,
Thanks for your help! appreciate it.
-
0
Hi,
Can i change data type CreatorUserId from long? to varchar ? How about if CreatorUserId is filled with username ? is this safe for other relation in module zero or abp core entities ?
-
0
You definitely should not do that, as CreatorUserId is handled by ABP. You can implement and manage your own CreatorUserName property.
-
0
Hi @aaron,
What do you mean with "implement and manage your own CreatorUserName property" ? can i extend auditEntity property to add CreatorUserName ? please advise.
-
0
What is your:
- use case, and
- expected behavior?
-
0
Hi,
I want to fill username when Repository.Insert() is executed and modify username when Repository.Update() executed. Field username will be stored in table. i want use this username info instead of id. can you help me how to extend base property in auditedEntity to add CreatorUserName and ModifierUserName?
-
0
- Add CreatorUserName and LastModifierUserName:
public class MyFullAuditedEntity : FullAuditedEntity { public string CreatorUserName { get; set; } public string LastModifierUserName { get; set; } }
- Subclass ClaimsAbpSession:
public class MyClaimsAbpSession : ClaimsAbpSession { public string UserName => PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == AbpClaimTypes.UserName); }
- Replace service in PreInitialize method of YourCoreModule:
Configuration.ReplaceService<IAbpSession, MyClaimsAbpSession>();
- Override methods in YourDbContext:
protected override void SetCreationAuditProperties(object entityAsObj, long? userId) { base.SetCreationAuditProperties(entityAsObj, userId); var myFullAuditedEntity = entityAsObj as MyFullAuditedEntity; if (myFullAuditedEntity != null) { myFullAuditedEntity.CreatorUserName = AbpSession.As<MyClaimsAbpSession>().UserName; } } protected override void SetModificationAuditProperties(object entityAsObj, long? userId) { base.SetModificationAuditProperties(entityAsObj, userId); var myFullAuditedEntity = entityAsObj as MyFullAuditedEntity; if (myFullAuditedEntity != null) { myFullAuditedEntity.LastModifierUserName = AbpSession.As<MyClaimsAbpSession>().UserName; } }