Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Albums has included Genre.So Album have genre and then genre has album list and it continues.Will it increase the size of dbcontext or will reduce performance. Is this the Wrong way to deal with loading of related navigation properties?


C#
[Bind(Exclude = "AlbumId")]
public class Album
  {
    [ScaffoldColumn(false)]
    public int AlbumId { get; set; }

    [DisplayName("Genre")]
    public int GenreId { get; set; }

    public virtual Genre Genre { get; set; }        

  }


   public partial class Genre
  {
    public int GenreId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public List<album> Albums { get; set; }
  }


 public ViewResult Index()
        {
            var albums = db.Albums.Include(a => a.Genre).Include(a => a.Artist);
            return View(albums.ToList());
        }




HTML
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Genre.Name)
        </td>
        <td>
            @Truncate(item.Artist.Name, 25)
        </td>
        <td>
            @Truncate(item.Title, 25)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Price)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.AlbumId }) |
            @Html.ActionLink("Details", "Details", new { id=item.AlbumId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.AlbumId })
        </td>
    </tr>

}
Posted
Updated 4-Jun-13 23:11pm
v2

1 solution

I'd argue that you don't need to have a list of Album in your Genre class, and instead you could have a method in the Album class that returns all albums of a provide genre.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900