Base solution for your next web application
Open Closed

rename column #4118


User avatar
0
verrrrrrrrrrrrrdi created

Hi,

How to rename base column in FullAuditedEntity like this: CreatorUserId -> InputUN CreationTime -> InputTime

please help..


10 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    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; }
    }
    
  • User Avatar
    0
    verrrrrrrrrrrrrdi created

    Hi @aaron,

    Thanks for your help, appreciate it!

    How to set value LastModificationTime and LastModifierUserId when Repository.Insert executed?

  • User Avatar
    0
    aaron created
    Support Team

    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);
    }
    
  • User Avatar
    0
    verrrrrrrrrrrrrdi created

    Hi @aaron,

    Thanks for your help! appreciate it.

  • User Avatar
    0
    verrrrrrrrrrrrrdi created

    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 ?

  • User Avatar
    0
    aaron created
    Support Team

    You definitely should not do that, as CreatorUserId is handled by ABP. You can implement and manage your own CreatorUserName property.

  • User Avatar
    0
    verrrrrrrrrrrrrdi created

    Hi @aaron,

    What do you mean with "implement and manage your own CreatorUserName property" ? can i extend auditEntity property to add CreatorUserName ? please advise.

  • User Avatar
    0
    aaron created
    Support Team

    What is your:

    • use case, and
    • expected behavior?
  • User Avatar
    0
    verrrrrrrrrrrrrdi created

    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?

  • User Avatar
    0
    aaron created
    Support Team
    1. Add CreatorUserName and LastModifierUserName:
    public class MyFullAuditedEntity : FullAuditedEntity
    {
        public string CreatorUserName { get; set; }
    
        public string LastModifierUserName { get; set; }
    }
    
    1. Subclass ClaimsAbpSession:
    public class MyClaimsAbpSession : ClaimsAbpSession
    {
        public string UserName => PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == AbpClaimTypes.UserName);
    }
    
    1. Replace service in PreInitialize method of YourCoreModule:
    Configuration.ReplaceService<IAbpSession, MyClaimsAbpSession>();
    
    1. 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;
        }
    }