I have three relationship tables, How can I read and show all of three tables data in a table using single line LINQ in index() MVC

[Table("Tag")]
    public class Tag
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int tagid { get; set; }
        public string tagname { get; set; }
        public int photoid { get; set; }

        public virtual Photo Photo { get; set; }
    }

[Table("Album")]
    public class Album
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int albumid { get; set; }
        public string albumname { get; set; }

        public virtual ICollection<Photo> Photos { get; set; }
    }

[Table("Photo")]
    public class Photo
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int photoid { get; set; }
        public string photoname { get; set; }
        public int albumid { get; set; }   
        
        public virtual Album Album { get; set; }
        public virtual ICollection<Tag> Tags { get; set; }       
    }

This is my model class:

public class MyPhotoModel
    {
        public IEnumerable<Photo> Photos { get; set; }
        public IEnumerable<Album> Album { get; set; }
        public ICollection<Tag> Tags { get; set; }

}

Hi,

It seems you are looking for
http://msdn.microsoft.com/en-us/data/jj574232#eagerLevels
 that allows to load related entities. Your model doesn’t seem to fit. You don’t want 3 separated lists but to keep the connection between an entity and its childs?

BTW avoid to ask the question in the subject line.

Hi hmsarab,

Thanks for your post.

I would suggest you read about Lazy Loading, Explicit Loading and Eager Loading.

http://www.entityframeworktutorial.net/EntityFramework4.3/eager-loading-with-dbcontext.aspx

http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application

If there’s anything else I can do for you regarding this matter,please feel free to post it in this forum.

Best Regards,

Eileen

Leave a Reply