首页 新闻 会员 周边

关于MvcMusicStore

0
悬赏园豆:5 [已关闭问题] 关闭于 2011-09-30 16:13

最近在学习Mvc3.0,MvcMusicStore项目,

在Models文件夹下,定义了很多类(Entity Framework, Code First部分),

其中,我找两个类的代码:

1. Genre类,代码如下:

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; }
}

2. Cart类,代码如下:

public class Cart
{
[Key]
public int RecordId { get; set; }
public string CartId { get; set; }
public int AlbumId { get; set; }
public int Count { get; set; }
public System.DateTime DateCreated { get; set; }

public virtual Album Album { get; set; }
}

如上面的红色行代码,及蓝色底色的virtual关键字,

问题:

1. virtual关键字用意?

2. 什么时候用?(有些类有,有些类没有,有些迷惑)

求解。

dboy8454的主页 dboy8454 | 初学一级 | 园豆:174
提问于:2011-08-04 16:49
< >
分享
所有回答(1)
0

virtual的作用,在于不确定本类或接口是否能满足继承类的所有需求时,加上它留待继承类中override,以重新构造相同参数的类或方法。

邀月 | 园豆:25475 (高人七级) | 2011-08-04 20:17
这里的virtual不是为了重载,
而是与Lazy Loading相关,可以参考下面的连接:
http://thedatafarm.com/blog/data-access/entity-framework-lazy-loading-by-context-or-by-property/
支持(0) 反对(0) dboy8454 | 园豆:174 (初学一级) | 2011-08-04 21:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册