Scaffolding with ASP.NET MVC4 & Entity Framework

Hi everyone,

I’m having some trouble with Entity Framework on ASP.NET MVC 4 and I’m hoping someone here can enlighten me. I’m following along with Professional ASP.NET MVC 4 and I’m trying to generate a Controller from a Model using EF. My Model class looks like so:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace MvcMusicStore.Models
{
    public class AlbumModels
    {
        public class Album
        {
            public virtual int AlbumId { get; set; }
            public virtual int GenreId { get; set; }
            public virtual int ArtistId { get; set; }
            public virtual string Title { get; set; }
            public virtual decimal Price { get; set; }
            public virtual string AlbumArtUrl { get; set; }
            public virtual Genre Genre { get; set; }
            public virtual Artist Artist { get; set; }
        }

        public class Artist
        {
            public virtual int ArtistId { get; set; }
            public virtual string Name { get; set; }
        }

        public class Genre
        {
            public virtual int GenreId { get; set; }
            public virtual string Name { get; set; }
            public virtual string Description { get; set; }
            public virtual List<Album> Albums { get; set; }
        }
    }
}

As per the instructions in the book, I right click my Controllers folder and choose Add > Controller and name it “StoreManagerController” choosing “MVC controller with read/write actions and views, using Entity Framework” as my template. I select “Album (MvcMusicStore.Models)” as my model and select “<New data context…>” as my Data context class and name it “MvcMusicStore.Models.MusicStoreDBContext” and click the Add button, which is when I run in to trouble.

I have installed Entity Framework using NuGet and restarted Visual Studio 2012 and built my solution but I get an error message saying:

There was an error generating ‘MvcMusicStore.Models.MusicStoreDBContext’. Try rebuilding your project.

Can anyone tell me what I’m doing wrong? I’ve followed everything in the book word for word but it doesn’t say what to name my file holding the models or what

using

statements to use. I’ve guessed that all I have to reference is

using System.Data.Entity;

?

Any help is appreciated :slight_smile: