none
How can I map ApplicationUser entity twice for different purposes. RRS feed

  • Question

  • In my case I have a Dogsitter platform that has appointments for both Owners and Dogsitters. But since I am using Individual users authentication I have the AspNetCoreUsers and AspNetCoreRoles tables and one Application user.

    public class ApplicationUser : IdentityUser, IAuditInfo, IDeletableEntity
    {
        public ApplicationUser()
        {
            // Application user
            this.Id = Guid.NewGuid().ToString();
            this.Roles = new HashSet<IdentityUserRole<string>>();
            this.Claims = new HashSet<IdentityUserClaim<string>>();
            this.Logins = new HashSet<IdentityUserLogin<string>>();
    
            // Owner/Dogsitter info
            this.Comments = new HashSet<Comment>();
            this.Dogs = new HashSet<Dog>();
            this.Appointments = new HashSet<Appointment>();
        }
    
        public string FirstName { get; set; }
    
        public string MiddleName { get; set; }
    
        public string LastName { get; set; }
    
        public string FullName => this.FirstName + this.MiddleName + this.LastName;
    
        public string ImageUrl { get; set; }
    
        public decimal Rating { get; set; }
    
        public string Address { get; set; }
    
        public string SelfDescription { get; set; }
    
        public string DogsDescription { get; set; }
    
        public decimal WageRate { get; set; }
    
        public Gender Gender { get; set; }
    
        public DateTime DateOfBirth { get; set; }
    
        public UserType UserType { get; set; }
    
        public ICollection<Comment> Comments { get; set; }
    
        public ICollection<Dog> Dogs { get; set; }
    
        public ICollection<Appointment> Appointments { get; set; }
    
        // Audit info
        public DateTime CreatedOn { get; set; }
    
        public DateTime? ModifiedOn { get; set; }
    
        // Deletable entity
        public bool IsDeleted { get; set; }
    
        public DateTime? DeletedOn { get; set; }
    
        public virtual ICollection<IdentityUserRole<string>> Roles { get; set; }
    
        public virtual ICollection<IdentityUserClaim<string>> Claims { get; set; }
    
        public virtual ICollection<IdentityUserLogin<string>> Logins { get; set; }
    }

    I want to map the same Application user twice in the appointment class like that:

    public class Appointment : BaseDeletableModel<int>
    {
        public bool IsHappening { get; set; }
    
        public int Timer { get; set; }
    
        public decimal TaxSoFar { get; set; }
    
        public DateTime Date { get; set; }
    
        public DateTime StartTime { get; set; }
    
        public DateTime EndTime { get; set; }
    
        public string OwnerId { get; set; }
    
        [ForeignKey("Id")]
        public virtual ApplicationUser Owner { get; set; }
    
        public string DogsitterId { get; set; }
    
        [ForeignKey("Id")]
        public virtual ApplicationUser Dogsitter { get; set; }
    }

    What I am asking is: Is it possible to do it this way or Is it logically incorrect? Should I create seperate classes Dogsitter and Owner and if yes how can I link them to being ApplicationUsers(How to link both of them to the ApplicationDbContext). Really struggling to wrap my head around this one. Thanks in advance.


    Thursday, March 26, 2020 12:53 AM

All replies

  • ASP.NET Core Identity can be discussed in Core forum in ASP.NET forums.

    http://forums.asp.net/

    Thursday, March 26, 2020 1:00 AM
  • Hi BlinkDrink,

    Thank you for posting here.

    Personally, I think it is not good.

    Although I don't know what your Dogsitter platform looks like, in theory, dog nanny and dog owner will definitely behave differently.

    According to the code you provided, you include all the behaviors of the two characters in one class, but some attributes may not be used by the dog owner, and some may not be used by the dog nanny.

    So, splitting it into two entity classes might be better.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 26, 2020 9:38 AM
  • Hello Timon Yang,

    Thank you for the answer, but could you help me understand how can I create two seperate classes DogNanny and Owner that both are apart from the AspNetCoreUsers table. Should they inherit ApplicationUser? 

    public class Dogsitter : ApplicationUser
        {
            public Dogsitter()
            {
                this.Appointments = new HashSet<Appointment>();
                this.Comments = new HashSet<Comment>();
            }
    
            public string Name { get; set; }
    
            public int Age { get; set; }
    
            public Gender Gender { get; set; }
    
            public DateTime DateOfBirth { get; set; }
    
            public string ImageUrl { get; set; }
    
            public string Description { get; set; }
    
            public decimal WageRate { get; set; }
    
            public string Address { get; set; }
    
            public decimal Rating { get; set; }
    
            public ICollection<Comment> Comments { get; set; }
    
            public ICollection<Appointment> Appointments { get; set; }
        }

    Something like this for Owner as well:

     public class Owner : ApplicationUser
        {
            public Owner()
            {
                this.Comments = new HashSet<Comment>();
                this.Dogs = new HashSet<Dog>();
                this.Appointments = new HashSet<Appointment>();
            }
    
            public string Name { get; set; }
    
            public string ImageUrl { get; set; }
    
            public decimal Rating { get; set; }
    
            public string Address { get; set; }
    
            public ICollection<Comment> Comments { get; set; }
    
            public ICollection<Dog> Dogs { get; set; }
    
            public ICollection<Appointment> Appointments { get; set; }
        }

    And this is the new ApplicationUser

     public class ApplicationUser : IdentityUser, IAuditInfo, IDeletableEntity
        {
            public ApplicationUser()
            {
                // Application user
                this.Id = Guid.NewGuid().ToString();
                this.Roles = new HashSet<IdentityUserRole<string>>();
                this.Claims = new HashSet<IdentityUserClaim<string>>();
                this.Logins = new HashSet<IdentityUserLogin<string>>();
            }
    
            // Audit info
            public DateTime CreatedOn { get; set; }
    
            public DateTime? ModifiedOn { get; set; }
    
            // Deletable entity
            public bool IsDeleted { get; set; }
    
            public DateTime? DeletedOn { get; set; }
    
            public virtual ICollection<IdentityUserRole<string>> Roles { get; set; }
    
            public virtual ICollection<IdentityUserClaim<string>> Claims { get; set; }
    
            public virtual ICollection<IdentityUserLogin<string>> Logins { get; set; }
        }

    So now what I want to understand is: How can I make the Owner and Dogsitter classes to behave like Users and be part of the users table without overlapping.

    Thursday, March 26, 2020 10:16 AM
  • So now what I want to understand is: How can I make the Owner and Dogsitter classes to behave like Users and be part of the users table without overlapping.

    They do some pretty wild things with ASP.NET Identity and autentication  in the ASP.NET forums where there is a Core forum. And MSDN community support people frequent the forums. I am not saying you're not getting good support here. But if it were me, I would get right in the horse's face,  smell its breath and ask the horse.

    Thursday, March 26, 2020 12:02 PM
  • I am sorry about that and thank you. I will do as you said.
    Thursday, March 26, 2020 2:12 PM