Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

How to Join AbpUsers table in EFCore #3275


User avatar
0
mikemey01 created

Hi - I'm using .net Core/Jquery v4.0

I have an entity that inherits FullAuditedEntity, meaning the CreatorUserID exists on this entity/table. How do I include the AbpUsers table when pulling data from the repository? Since the CreatorUserID column is not setup as a foreign key to the AbpUsers.Id I'm not sure how to reference it correctly. I'm trying to pull in the username from the AbpUsers table into my entity.

Thanks,

Mike


8 Answer(s)
  • User Avatar
    0
    mikemey01 created

    For reference here is what I'm using:

    I'm subclassing AbpUsers<User> and using the InverseProperty attribute:

    public class User : AbpUser<User>
        {
            [InverseProperty("CreatorUser")]
            public ICollection<PMNotes> CreatorNotes { get; set; }
            [InverseProperty("DeleterUser")]
            public ICollection<PMNotes> DeleterNotes { get; set; }
            
        }
    

    then in the table that I want to reference I'm doing:

    [Table("PMNotes")]
        public class PMNotes : FullAuditedEntity, IMustHaveTenant
        {
            public virtual int TenantId { get; set; }
    
            public virtual User CreatorUser { get; set; }
            public virtual User DeleterUser { get; set; }
            
            [Required]
            public virtual string Note { get; set; }
        }
    

    but i get this error:

    Unable to determine the relationship represented by navigation property 'User.DeleterUser' of type 'User'. Either manually configure the relationship, or ignore this property from the model.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Does your scenario works for CreatorUser when you remove DeleterUser InverseProperty ?

    Thanks.

  • User Avatar
    0
    mikemey01 created

    Hi - unfortunately not, I get the following error:

    Unable to determine the relationship represented by navigation property 'User.DeleterUser' of type 'User'. Either manually configure the relationship, or ignore this property from the model.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you define your entity like this ?

    public class PMNotes : FullAuditedEntity<User>, IMustHaveTenant
    {
    ...
    }
    

    You will need to remove CreatorUser and DeletorUser as well.

    If it does not work, you can try to configure it manually.

    Thanks.

  • User Avatar
    0
    mikemey01 created

    That breaks the app service:

    private readonly IRepository<PMNotes> _notesRepository;

    with the error:

    The type ...PMNotes can not be used as a type parameter 'TEntity' in the generic type or method IRepository<TEntity>.

    what is the manual configuration look like?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you change your repository to

    private readonly IRepository<PMNotes,User> _notesRepository;
    

    Actually I don't know the manual configuration but you have to do it in OnModelCreating on your DbContext. You can check it on the internet since it is a common thing in EF.

    Thanks.

  • User Avatar
    0
    mikemey01 created

    I got this working where I can now include the AbpUsers table for any AuditedEntity by adding a FK to either CreatorUserId, DeleterUserId, or LastModifierUserId.

    Table with CreatorUserId, note that it references Authorization.Users.User instead of subclassing the AbpUsers entity:

    [Table("PMNotes")]
        public class PMNotes : FullAuditedEntity, IMustHaveTenant
        {
            public virtual int TenantId { get; set; }
            
             ...
    
            [ForeignKey("CreatorUserId")]
            public Authorization.Users.User CreatorUser { get; set; }
        }
    

    Added the collection to the Authorization.Users.User table:

    public class User : AbpUser<User>
        {
            public ICollection<PMNotes> CreatorNotes { get; set; }
            ...
         }
    

    Then in the DBContext, add the following (although this may not actually be required with the "ForeignKey" attribute above:

    modelBuilder.Entity<PMNotes>()
                            .HasOne(m => m.CreatorUser)
                            .WithMany(t => t.CreatorNotes)
                            .HasForeignKey(m => m.CreatorUserId)
                            .OnDelete(DeleteBehavior.Restrict);
    

    Hope it helps someone.

  • User Avatar
    0
    jdavis01 created

    @MikeyMey01,

    Helped a great deal. Thanks for posting..