2

I have just installed MVC4.

According to nuGet package manager, my version of EF is 5.0.0, last updated on 11th Sept 2012

When I try to create a new controller (right click controllers -> Add -> Controller), I am faced with an error message and am not able to create a new controller...

Could not load type 'System.ComponentModel.DataAnnotations.Schema.TableAttribute' 
from assembly 'EntityFramework, Version=4.1.0.0, Culture=neutral,     
PublicKeyToken=b77a5c561934e089'.

I am just getting started with ASP.NET MVC, so if anybody could help me to resolve, it would be much appreciated.

From the Package Manager Console, I have tried uninstalling and re-installing Entity Framework as follows but this has had no effect:

PM> Uninstall-Package EntityFramework
Successfully removed 'EntityFramework 5.0.0' from GD.
Successfully uninstalled 'EntityFramework 5.0.0'.

PM> Install-Package EntityFramework
You are downloading EntityFramework from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=253898&clcid=0x409. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'EntityFramework 5.0.0'.
Successfully added 'EntityFramework 5.0.0' to GD.

Type 'get-help EntityFramework' to see all available Entity Framework commands.

PM> 

Many thanks in advance for all your help.

UPDATE:

I am following this tutorial. http://msdn.microsoft.com/en-us/data/gg685467.aspx

It appears that MVC4 does not like this method of creating a DBContext...

Also there will be a new reference listed in the project references
called EntityFramework. This is the assembly that contains the Code
First runtime.

Add a new class to the Models folder and name it BlogContext. Modify
the class to match the following code: 

using System.Data.Entity;
namespace MVC3AppCodeFirst.Models 
{   
    public class BlogContext : DbContext   
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
        public DbSet<Comment> Comments { get; set; }   
    } 
}

It builds fine, so am I missing something ???

5
  • 1
    Did you try to clean up your solution? (right click on solution->Clean Solution) Commented Sep 27, 2012 at 18:55
  • For some reason, you have an EF 4.1 reference in your web.config. Is this a new project, or are you adding EF5 to an existing EF4 project? Commented Sep 27, 2012 at 20:13
  • This is a BRAND new project. That's why I thought it was a configuration error. I installed EF5, but it appears that all references are to EF4??? I don't think it installed properly... Commented Sep 27, 2012 at 20:27
  • @Gravy - is this Visual Studio 2010 or 2012? Commented Sep 27, 2012 at 23:11
  • Could it be that you accidently installed MVC3 rather than MVC4? Commented Sep 27, 2012 at 23:15

3 Answers 3

2

In your visual studio solution explorer, go to Models folder and open AccountModels.cs file. Comment out the following line

[Table("UserProfile")]

after commenting out, it will become

//[Table("UserProfile")]

Now compile and try to add a new controller.

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

1 Comment

perfect! It was this trivial detail that was missing.
0

If you project targets .NET 4.0, EF will use version 4.4 which is EF5 version with functionality scoped down to what core components in .NET Framework 4 can support (i.e. you will not get support for enums or spatial types). Your MVC project needs to target 4.5 if you want to use all new features shipped with EF5.

2 Comments

EF5 itself will not downgrade to 4.1. If you install EF5 package from nugget it contains two versions of EntityFramework.dll - one that works with .NET Framework 4.5 and one that works with .NET Framework4 (obviously with limited functionality) but they both are still EF5. To clarify MVC project might do something along these lines (I don't know) but it's not EF itself.
I edited my comment to reflect what VS2012 reports ... which is Entity Framework version 4.4.0.0 when you add EF5 from NuGet into a .NET 4 project. All I was pointing out is the difference between EF5 w/.NET 4.5 and EF4.4 w/.NET 4.0.
0

Problem solved...

  • Uninstall Visual Studio 2010
  • Install Visual Studio 2012 Ultimate

Job done!!!

:)

1 Comment

No you did not solve it u found a workaround, now I have the same issue but upgrading is not an option for me :(

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.