In this task, you will update the StoreController class so that instead of using hard-coded data, it queries the database to retrieve all its information. Open Controllers\StoreController.cs and add the following field to the class to hold an instance of the MusicStoreEntities class, named storeDB: (Code Snippet ASP.NET MVC Models and Data Access Ex1 storeDB CSharp) – – – C# public class StoreController : Controller { MusicStoreEntities storeDB = new MusicStoreEntities(); The MusicStoreEntities class exposes a collection property for each table in the database. Update StoreController’s Index action method to retrieve all Genre names in the database. This was done previously by hard-coding string data. Now you can instead write a LINQ query expression like below which retrieves the Name property of each Genre within the database: (Code Snippet ASP.NET MVC Models and Data Access Ex1 Store Index CSharp) – – – C# // // GET: /Store/ public ActionResult Index() { // Retrieve the list of genres var genres = from genre in storeDB.Genres select genre.Name; // Create your view model } Note: You are using a capability of .NET called LINQ (language-integrated query) to write strongly-typed query expressions against these collections which will execute code against – the database and return objects that you can program against. For more information about LINQ, please visit the . Transform the collection of genres to a list. To do this, replace the following code: (Code Snippet ASP.NET MVC Models and Data Access Ex1 Store Index ToList CSharp) – – – C# public ActionResult Index() {
In this task, you will update the StoreController class so that instead of using hard-coded data, it queries the database to retrieve all its information. Open Controllers\StoreController.cs and add the following field to the class to hold an instance of the MusicStoreEntities class, named storeDB: (Code Snippet – ASP.NET MVC Models and Data Access – Ex1 storeDB – CSharp) C# public class StoreController : Controller { MusicStoreEntities storeDB = new MusicStoreEntities(); The MusicStoreEntities class exposes a collection property for each table in the database. Update StoreController’s Index action method to retrieve all Genre names in the database. This was done previously by hard-coding string data. Now you can instead write a LINQ query expression like below which retrieves the Name property of each Genre within the database: (Code Snippet – ASP.NET MVC Models and Data Access – Ex1 Store Index – CSharp) C# // // GET: /Store/ public ActionResult Index() { // Retrieve the list of genres var genres = from genre in storeDB.Genres select genre.Name; // Create your view model } Note: You are using a capability of .NET called LINQ (language-integrated query) to write strongly-typed query expressions against these collections – which will execute code against the database and return objects that you can program against. For more information about LINQ, please visit the . Transform the collection of genres to a list. To do this, replace the following code: (Code Snippet – ASP.NET MVC Models and Data Access – Ex1 Store Index ToList – CSharp) C# public ActionResult Index() {
ASPNETMVC3ModelsAndDataAccess.docx