Base solution for your next web application
Open Closed

UserRole and Role names #2415


User avatar
0
gpcaretti created

Dear All,
I had the same issue shown here: UserRole topic and solved in a similar way with the payload of writing complex code.

My question is: Why don't you add a reference to Roles in UserRole class?

I post here your original code for UserRole with my little suggestion:

public class UserRole : CreationAuditedEntity, IMayHaveTenant
{
        public virtual int? TenantId { get; set; }
        public virtual long UserId { get; set; }
        public virtual int RoleId { get; set; }

       // my suggestion
        [ForeignKey("RoleId")]
        public virtual Role Role { get; set; }

       public UserRole()
        {
        }

        public UserRole(int? tenantId, long userId, int roleId)
        {
            TenantId = tenantId;
            UserId = userId;
            RoleId = roleId;
        }     
}

1 Answer(s)