I was able to successfully implement the following solution. Do you see any red flags?
src\aspnet-core\src\Company.App.EntityFrameworkCore\EntityFrameworkCore\AppDbContext.cs
namespace Company.App.EntityFrameworkCore
{
public class AppDbContext : AbpZeroDbContext<Tenant, Role, User, AppDbContext>, IAbpPersistedGrantDbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
ChangeTracker.StateChanged += OnEntityStateChanged;
}
private void OnEntityStateChanged(object sender, EntityStateChangedEventArgs e)
{
if (e.Entry.Entity is UserRole && e.NewState == EntityState.Deleted)
{
e.Entry.State = EntityState.Modified;
e.Entry.CurrentValues["IsDeleted"] = true;
e.Entry.CurrentValues["DeletionTime"] = DateTime.Now;
e.Entry.CurrentValues["DeleterUserId"] = AbpSession.UserId;
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<UserRole>().HasQueryFilter(p => !EF.Property<bool>(p, "IsDeleted"));
modelBuilder.Entity<UserRole>().Property<bool>("IsDeleted");
modelBuilder.Entity<UserRole>().Property<DateTime?>("DeletionTime").IsRequired(false);
modelBuilder.Entity<UserRole>().Property<long?>("DeleterUserId").IsRequired(false);
}
}
}
Hi maliming,
The project has been built upon the "<PROJECTNAME>-aspnetzero-7.3.1" ZIP archive and is still using EF Core 2.2.6.
~~Also, I couldn't find a way to get the user behind any of the entity events. Is this even possible?~~
~~I found out I can inject AbpSession to get the current user.~~
Turns out AbpSession is already part of the base class.
Hello,
When a role is removed from a user, we need to track who did it (ie. which AbpUser) and when they did it.
The obvious solution is to redefine the UserRole entity so that it inherits from FullAuditedEntity instead of CreationAuditedEntity, but the UserRole entity is defined in a nuget package so we cannot simply change the definition.
Is there a way to achieve this behavior that I am not seeing?
Also, in this post ismcagdas said "You cannot change UserRole to SoftDelete".
Is this also because the UserRole entity is defined in a nuget package, or is there another reason it cannot be done?
Here is what I have tried so far.
Approach 1: I tried handling this at the database level by setting up a delete trigger on the AbpUserRole table which would insert a record into a AbpUserRoleDeleted table, but I can't think of a way to find out which AbpUser made the deletion with this approach. I can only track when the action happened.
Approach 2: I tried listening for the EntityDeleted domain event on UserRole entities, but it does not seem to get triggered. Interestingly, the EntityUpdated event is triggered when I remove a role from a user, but even assuming that this event would only ever be triggered when a UserRole is deleted, the event data still does not include who made the deletion. If it did, I could manually save the audit information in a separate table just like a database delete trigger would, but this time I would have the AbpUser that was responsible for the deletion.
Approach 3: I tried extending the UserRole entity by following the steps here. I was able to implement the IDeletionAudited interface and generate a migration that creates the associated columns on the AbpUserRoles table, but removing a role from a user performs a hard delete instead of a soft delete so I can't tell if the columns even get populated. I am assuming they do not.
Approach 4: I tried enabling Entity History for the UserRole entity, but it seems to only track when a UserRole entity is created.
I will continue exploring more approaches, but in the meantime I would appreciate any help you can provide with this problem.
Thanks!
Hi ismcagdas,
That totally worked. Thanks!
I want to download v7.3.0 of my existing project from the "Your recent downloads" area at https://aspnetzero.com/Download, but my only options are to download the current version of my project (ie. v7.0.0) or the latest version that is currently available (v8.0.0 at the time I am writing this). Is there a way to download v7.3.0, or even v7.2.0 or v7.1.0 if I so desired?
OK I got it working by deleting the github account from the license management and re-adding it.
For our account [email protected], we are unable to download Metronic 6.x. We get an error "Not found". If we go directly to github.com/orgs/aspnetzero
We only see the "Documents" repository.
Can you help?