Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Surprise, surprise! Here we find ourselves in Visual Studio again. Let's update the data model now to have it access to database to do the querying to build our initial set of items and to implement the logic we need for the details page. So move over here is the Solution Explorer, open up the MainViewModel.cs, and put our new version in and see what we've changed. To start with, we have a local intense variable, we're going to hold the Sonnets dbDataContext.
And in our MainViewModel Constructor we're going ask the App class for the instances of the database that we've created there. Also as we did before, we're going to create art to ObservableCollection of ItemsViewModels for items and favorite items. All that remains the same. What's going to change now is the logic inside load data. So instead of looking at the data that we got from XML--which is what we did before--we're simply going to query the database for the Sonnets.
So the way you do queries using the SQL is to simply say var sonnetsInDB equals from sonnets, variable S, in dbDataContext.Item--remember that was the table we defined over layer in dbData Context. Select this and so that the equivalent of select star from Sonnets in the database. And now that we have that, we're going to create an observable collection of those sonnets in order to be able to iterate over them and create our ItemViewModel, and we'll simply then company properties from the Sonnet Objects into the ItemViewModel objects.
And then we added to the items list and again to the favorites list if it is a favorite sonnet, and that's all we need to do to love the data from the database. You can also see that add item to favorites and remove items from favorites are the same as what we use for the flat file version of Sonnets plus. But you might notice that the update database method is missing, and that's because we're going to use link to SQL over in the details page in order to do the actual updates. We don't need to do the updates here because we're not managing the XML file anymore.
So let's move on to updating the details page and fire this puppy up and see if it runs.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 104748 Viewers
56 Video lessons · 116631 Viewers
71 Video lessons · 85816 Viewers
131 Video lessons · 41060 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.