Why am I getting compilation errors in a new MVC4 project?

11,498

Solution 1

Simply Reinstall EntityFramework:

  1. Go to Package Manager Console
  2. Uninstall-Package entityframework
  3. Install-Package entityframework

that is it the error should go away!

Solution 2

In fact, it is not a problem of MVC4 template project but of changing the target .Net Framework.

When you create a new MVC4 project in VS2012, it will (by default) reference .Net 4.5. All files are generated based upon this version. The AccountModel use new attributes from System.ComponentModel.DataAnnotations (TableAttribute, DatabaseGeneratedAttribute, ...

Retargetting the framework version only change references and does not affect source code : attributes from 4.5 framework will not be removed.

If you want a MVC4 projet with .Net 4.0, you could :

  • Removed .net 4.5 attributes from AccountModel
  • Remove AccountMode if it is not require
  • Select .NET 4.0 in the Create a New Project Wizard.

Solution 3

The Uninstall-Package entityframework / Install-Package entityframework solution didn't work for me. Instead I had to open the .csproj file in notepad and do a find and replace "net40" to "net45" to force it to load the .net 4.5 version of all the packages. This made the correct version of EntityFramework load as well.

Share:
11,498

Related videos on Youtube

ComputerG33k
Author by

ComputerG33k

Updated on September 15, 2022

Comments

  • ComputerG33k
    ComputerG33k over 1 year

    I haven't been able to find an answer to this and have been scouring the internet all weekend, so I was hoping maybe you could help me out with this.

    When using Visual Studio 2012, I create a new MVC4 Web Application Project and get some compilation errors.

    Here is my problem:

    1. I create a new MVC4 Web Application using Visual Studio 2012, I select Internet Application and set it to use RAZOR Engine.

    2. I go to Application Properties and under the Application Tab I change Target Framework from .NET Framework 4.5 to .NET Framework 4.

    3. The build produces the following errors:

      Error 1 The type name 'TableAttribute' could not be found. This type has been forwarded to assembly 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Consider adding a reference to that assembly. c:\users\saad\documents\visual studio 2012\Projects\MvcApplication1\MvcApplication1\Models\AccountModels.cs 21 6 MvcApplication1

      Error 2 The type or namespace name 'Table' could not be found (are you missing a using directive or an assembly reference?) c:\users\saad\documents\visual studio 2012\Projects\MvcApplication1\MvcApplication1\Models\AccountModels.cs 21 6 MvcApplication1

      Error 3 The type or namespace name 'DatabaseGeneratedAttributeAttribute' could not be found (are you missing a using directive or an assembly reference?) c:\users\saad\documents\visual studio 2012\Projects\MvcApplication1\MvcApplication1\Models\AccountModels.cs 25 10 MvcApplication1

      Error 4 The type name 'DatabaseGeneratedAttribute' could not be found. This type has been forwarded to assembly 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Consider adding a reference to that assembly. c:\users\saad\documents\visual studio 2012\Projects\MvcApplication1\MvcApplication1\Models\AccountModels.cs 25 10 MvcApplication1

      Error 5 The type or namespace name 'CompareAttribute' could not be found (are you missing a using directive or an assembly reference?) c:\users\saad\documents\visual studio 2012\Projects\MvcApplication1\MvcApplication1\Models\AccountModels.cs 54 10 MvcApplication1

      Error 6 The type or namespace name 'Compare' could not be found (are you missing a using directive or an assembly reference?) c:\users\saad\documents\visual studio 2012\Projects\MvcApplication1\MvcApplication1\Models\AccountModels.cs 54 10 MvcApplication1

      Error 7 The type or namespace name 'CompareAttribute' could not be found (are you missing a using directive or an assembly reference?) c:\users\saad\documents\visual studio 2012\Projects\MvcApplication1\MvcApplication1\Models\AccountModels.cs 87 10 MvcApplication1

      Error 8 The type or namespace name 'Compare' could not be found (are you missing a using directive or an assembly reference?) c:\users\saad\documents\visual studio 2012\Projects\MvcApplication1\MvcApplication1\Models\AccountModels.cs 87 10 MvcApplication1

  • Sunny Sharma
    Sunny Sharma over 10 years
    +1 mark it as answer, thanks @Stacker for sharing, Thumbs Up!
  • DeveloperDan
    DeveloperDan almost 9 years
    Beware: Package manager console commands appear to be case sensitive or picky about spacing. This answer initially failed for me until I typed it exactly as shown.