@Model.Genre.Name

Apr 7, 2012 at 8:59 PM

Hi, i`ve been following this tutorial which great btw, but i stuck at the updated album details. It doesnt retrieve any data here @Model.Genre.Name so i have exception which i cant handle... here is the exception

System.NullReferenceException was unhandled by user code  Message=Object reference not set to an instance of an object.  Source=App_Web_qa3wfhx1  StackTrace:       at ASP._Page_Views_Store_Details_cshtml.Execute()

i probalby missed something cant figure out what...

Apr 7, 2012 at 9:07 PM

Its solved by adding to Album class  virtual keyword on Genre and Artist, but i still dont understand what exactly happened here. Can u provide me some explanation please. Tnx in advance.

public virtual Genre Genre { get; set; }       

public virtual Artist Artist { get; set; }

Coordinator
Apr 9, 2012 at 2:51 AM

@zenmorke

This is a convention in Entity Framework that enables lazy loading the related data when accessed:

One of the requirements for lazy loading proxy creation is that the navigation properties must be declared virtual (Overridable in Visual Basic). If you want to disable lazy loading for only some navigation properties, then make those properties non-virtual.

http://msdn.microsoft.com/en-us/library/gg715120(v=vs.103).aspx


From: zenmorke [notifications@codeplex.com]
Sent: Saturday, April 07, 2012 1:07 PM
To: Jon Galloway
Subject: Re: @Model.Genre.Name [mvcmusicstore:351535]

From: zenmorke

Its solved by adding to Album class virtual keyword on Genre and Artist, but i still dont understand what exactly happened here. Can u provide me some explanation please. Tnx in advance.

public virtual Genre Genre { get; set; }

public virtual Artist Artist { get; set; }

Jul 3, 2012 at 11:08 PM

Hi Jon,

I'm a total Newbi here in MS MVC land; coming from Drupal land.  Not only is this (MVC) totally awesome in comparison, but this tutorial is a great step by step HowTo article.  I've been successfully following the Tutorial v3.0.pdf doc page by page using VS2010. Thank you so much!

I came to this post looking for the what and why of the virtual keyword and your "lazy-loading" reference.  "lazy-loading" is not in my vocabulary and the formal documentation reference is way to hard to follow.  As a test, I deleted 'virtual' and the ill-effect I saw was that the /details page did not include the genre nor artist info for the albumID.  So, correct me if I'm wrong, but I'm guessing that somehow 'virtual' tells the system to relationally fetch the Genre and Artist fields from the database, keyed off the AlbumID. Is this correct? 

The pdf could use a short, plain English, explanation of the 'virtual' keyword and it's usage within the Album model.

Thanks again,

Bob

 

Jul 4, 2012 at 1:19 PM
@ bmarcotte

Hi, you can use this tutorial to get your answer

Jul 5, 2012 at 3:54 PM

@zenmorke

Thanks, part 5 of that tutorial explained it nicely.