I'm developing a simple application that will allow users to create a profile and upload multiple images. I'm using ASP.NET Identity and will therefore refer to "user" as ApplicationUser. I have an Image and an ApplicationUser domain model shown below:

    public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
        public string UserFirstName { get; set; }
        public string UserSurname { get; set; }
        public string City { get; set; }
        public virtual ICollection<Image> Image { get; set; }
    }

And

    public class Image
    {
        public int ImageID { get; set; }
        public string ImageCaption { get; set; }
        public int NumOfLikes { get; set; }
        public string ImageFilePath { get; set; }
        //[ForeignKey("ApplicationUser")]
        //public int ApplicationUserID { get; set; }
        public virtual ApplicationUser ApplicationUser { get; set; }
    }

I added public virtual ICollection<Image> Image { get; set; } and public virtual ApplicationUser ApplicationUser { get; set; } to define a one-to-many relationship between ApplicationUser and Image. After running the application I noticed that Entity Framework had created ApplicationUser_id in the Images table (Which I assume I can use as a foreign key without having to uncomment the code in the Image domain model).

I also have ViewImagesViewModel which will return to a view, a collection of images which I would like to be able to filter and sort by ApplicationUser's name,surname and city. My problem is that I don't know how to combine the properties from both domain models into one view model with sortable/filterable images (by name, surname and city) since I can't sort by ApplicationUser_id.


Questions

  1. Should I add public string UserFirstName { get; set; }, public string UserSurname { get; set; } and public string City { get; set; } to my Image domain model then map this to my view model or is there a way to achieve this from ApplicationUser_id (for example, just querying the name, surname and city from ApplicationUser_id?
  2. If I run a query to obtain the name, surname and city of each images owner, how would I associate each users details to an image so that a single view model is returned to the view?

Related posts

Recent Viewed