Shortcut method of building a Code First project

Jun 30, 2012 at 2:42 AM
Edited Jul 2, 2012 at 10:54 PM

To Jon Galloway,

Jon I would like to express my appreciation for your MusicStore tutorial, it has been a great help to me in learning MVC, EF and Razor. I am not a developer, but I like programming with VS 2010 Express, Visual Basic, WPF, MVC, EF and Razor; this is my hobby. Since I retired in 1997 my vocation has been my MVC website at bobagans.org, and since you state on the Acknowledgments page of Professional ASP.NET MVC 3 that you look to "Philippians 4:4-9 for continually reminding me which way is up," you may find it of some interest.

The way that I use your tutorial is to copy your instructions in a word processor file, then I translate the C# code into VB and add it to my file; this gives me a chance to become more acquainted with C#, Razor and what the various sections of code accomplish in the application. I downloaded a VB version of MusicStore so I can compare them and learn. I realize that it would require a book to fully explain your tutorial, so I have settled down to doing some research and experimenting which lead me to using the shortcut method below. Many thanks for the effort you have put into developing this great and thorough tutorial.

Bob

Here’s a shortcut method to building a Code First project. Instead of starting with an empty project template, as Jon does in his tutorial, we will start with an Internet Application project template, just as Jon does in the book he coauthored, Professional ASP.NET MVC 3 on page 70. We then create the Classes and StoreManagerController before we create the StoreController. Here are the steps that I used:

1. Build a new MvcMusicStore project with an Internet Application template, and add the latest Entity version

2. Add an Artist class and add the code in Jon's tutorial

3. Add a Genre class and add the code in Jon's tutorial

4. Add an Album class and add the code in Jon's tutorial

5. Build your project

6. Add a StoreManagerController with:

a. Template = read and write actions and views

b. Model class = Album (MvcMusicStore.Models)

c. Data context class = click New Data Context, then MvcMusicStore.Models.MusicStore.Context.

 7. Run the application and browse to the StoreManager, then close down your app and add the next 4 blocks of code to the StoreManagerController that was created.

        '
        ' Get: /StoreManager/CreateGenre

        Function CreateGenre() As ViewResult
            Return View()
        End Function

        '
        ' Post: /StoreManager/CreateGenre

        <HttpPost()>       
        Function CreateGenre(genre As Genre) As ActionResult
            If ModelState.IsValid Then
                db.Genres.Add(genre)
                db.SaveChanges()
                Return RedirectToAction("Index")
            End If

        End Function

        '
        ' Get: /StoreManager/CreateArtist

        Function CreateArtist() As ViewResult
            Return View()
        End Function

        '
        ' Post: /StoreManager/CreateArtist

        <HttpPost()>
        Function CreateArtist(artist As Artist) As ActionResult
            If ModelState.IsValid Then
                db.Artists.Add(artist)
                db.SaveChanges()
                Return RedirectToAction("Index")
            End If

        End Function

 8. Then add the view for creating Genres, right click in the CreateGenre method and choose AddView, check create a strongly-typed view, in the Model class check Genres (MvStore), in the Scaffold template choose Create and then add the view.

 9. Do the same for the Artist view except for checking Artists in the Model class.

 10. In the StoreManager Index view substitute the 3 ActionLinks below for the one that was created.

    @Html.ActionLink("Create New Album___", "Create")
    @Html.ActionLink("Create New Genre___", "CreateGenre")
    @Html.ActionLink("Create New Artist___", "CreateArtist")

11. When you follow the above way of creating your project you won’t have to use any method to seed your database or link to it. You can merely browse to StoreManager, click on the Create New Genre ActionLink to add your Genres, click on the Create New Artist to add your Artists, and then click on Create New Album to add your Albums.

12. Any changes you make that requires an adjustment to the database can be handled through Code First Migrations.

  Note: I found that if for any reason I viewed the database that EF created in step 7 above with SQL Server Management Studio or any other, I had to close down my computer, not just the application, and reboot or I would encounter an exception after I viewed the database. I’m using VS 2010 Express so maybe this doesn’t happen with the other versions.

 Bob