0

I'm following along with "Professional ASP.NET MVC 4" and trying to generate a controller from a model using Entity Framework. My Model looks like this:

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

namespace MvcMusicStore.Models
{
    public class MusicStoreModels
    {
        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; }
        }
    }
}

When I right click my Controllers folder and choose Add > Controller I select "MVC controller with read/write action and views, using Entity Framework" as my template and "Album (MvcMusicStore.Models)" as my Model class. The book tells me to select "new data context..." and name it "MvcMusicStore.Models.MusicStoreDBContext".

Everything looks OK and I have saved and built my solution prior to performing the above actions. However, I get an error message saying

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

I'm at a bit of a loss. Can anyone help?

5
  • Why is every property virtual? Commented Jan 4, 2013 at 18:08
  • Honestly, I don't know. To quote the book: "You might also notice that every property is virtual. I discuss why the properties are virtual later in this chapter. For now, these three simple class definitions are your starting models, and include everything you need to scaffold out a controller and some views and even create a database." Commented Jan 4, 2013 at 18:14
  • virtual properties assist with lazy loading of entities. Commented Jan 4, 2013 at 18:39
  • I've installed Entity Framework 5 via NuGet in Visual Studio 2012 but still having the same problem when I try to generate my Controller. Am I missing a using statement somewhere? Feeling pretty frustrated. Commented Jan 4, 2013 at 18:55
  • I wouldn't have the primitive properties (int, string , decimal) as virtual, there's no point in lazy-loading them. Commented Nov 14, 2013 at 8:39

2 Answers 2

1

The symptoms sound a lot like this issue: http://www.rhysgodfrey.co.uk/archive/2011/04/20/mvc3-tools-update-and-entity-framework-4-1-error.aspx

I would suggest uninstalling all versions of Entity Framework, re-installing the latest version through NuGet, and regenerating your EF context from scratch.

Sign up to request clarification or add additional context in comments.

Comments

0

I not very up on C# (i think it is C# code - but dont quote me on that).....

Well after pasting your code in a new project i notice the line that says

Using System.Data.Entity;

Is flagged as an error, And some further research shows that a refrence is POSSIBLY missing in inyour config.sys file...

Re: http://forums.asp.net/t/1381740.aspx/1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.