[MVC] model类中Virtual的作用

在学习MusicStore教程中,执行程序->查看唱片详细信息时,弹出以下异常:



代码如下:

Model层Album类结构为:

 public class Album
    {
        public int AlbumId { get; set; }
        public int ArtistId { get; set; }
        public int GenreId { get; set; }
        public string Title { get; set; }
        public decimal Price { get; set; }
        public string AlbumArtUrl { get; set; }

        public Genre Genre { get; set; }
        public Artist Artist { get; set; }
    }

Controller层显示该界面的方法为:

 public ActionResult Details(int id)
        {
            var album = storeDB.Albums.Find(id);
            return View(album);
        }
因为异常显示Genre未实例化,所以开始的解决办法是在Details方法里,将

var album = storeDB.Albums.Find(id);

改为

 var album = storeDB.Albums.Include("Genre").Include("Artist").Single(item => item.AlbumId == id);
后,界面显示正常。但是有个疑问——MusicStore教程中并没有这样写呀...

仔细对照代码后,教程中发现Album类关于Genre和Artist的定义中各多了Virtual修饰符,更改后原来的代码执行成功!

关于Virtual,教程中的原文如下:

While we’re there, we’ve also changed the Genre and Artist to virtual properties. This allows Entity Framework to lazy-load them as necessary.

理解为:使用了virtual的类或类集合的好处在于延时加载,只有在使用该数据时,EF再去自动执行数据加载操作。


最后Model层代码如下:

 public class Album
    {
        public int AlbumId { get; set; }
        public int ArtistId { get; set; }
        public int GenreId { get; set; }
        public string Title { get; set; }
        //[Required(ErrorMessage="Price is required!")]
        public decimal Price { get; set; }
        public string AlbumArtUrl { get; set; }

        public virtual Genre Genre { get; set; }
        public virtual Artist Artist { get; set; }
    }

Controller中的代码维持原来的不变.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值