已安装 MVC 4 实体框架配置错误



我刚刚安装了MVC4。

根据 nuGet 包管理器,我的 EF 版本是 5.0.0,最后更新于 2012 年 9 月 11 日

当我尝试创建一个新控制器(右键单击控制器 -> 添加 -> 控制器)时,我遇到了一条错误消息,无法创建新控制器...

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

我刚刚开始使用 ASP.NET MVC,所以如果有人可以帮助我解决,将不胜感激。

在包管理器控制台中,我尝试卸载并重新安装实体框架,如下所示,但这没有效果:

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> 

提前感谢您的所有帮助。

更新:

我正在学习本教程。 http://msdn.microsoft.com/en-us/data/gg685467.aspx

看来 MVC4 不喜欢这种创建 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; }   
    } 
}

它构建得很好,所以我错过了一些???

在 Visual Studio solution explorer 中,转到Models文件夹并打开AccountModels.cs文件。注释掉以下行

[Table("UserProfile")]

注释掉后,它将变成

//[Table("UserProfile")]

现在编译并尝试添加新的控制器。

如果项目面向 .NET 4.0,EF 将使用版本 4.4,即

EF5 版本,其功能范围缩小到 .NET Framework 4 中的核心组件可以支持的内容(即,您将不支持枚举或空间类型)。如果要使用 EF5 附带的所有新功能,MVC 项目需要面向 4.5。

问题已解决...

  • 卸载 Visual Studio 2010
  • 安装 Visual Studio 2012 Ultimate

工作完成!!

:)