一对多并加载相关实体 [英] One to Many and loading related entities

查看:61
本文介绍了一对多并加载相关实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

专辑已包含Genre.So专辑有流派,然后流派有专辑列表并继续。它会增加dbcontext的大小或会降低性能。这是处理相关导航属性加载的错误方法吗?





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?


[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());
        }













@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>



}


}

推荐答案

我认为你不需要在你的Genre类中有一个Album列表,而是你可以在Album类中有一个返回所有专辑的方法提供类型。
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.


这篇关于一对多并加载相关实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆