【问题标题】:Installed MVC 4 Entity framework configuration errors安装 MVC 4 Entity 框架配置错误
【发布时间】:2013-11-12 13:59:53
【问题描述】:

我刚刚安装了 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; }   
    } 
}

它构建得很好,所以我错过了什么???

【问题讨论】:

  • 您是否尝试清理您的解决方案? (右键解决方案->清理解决方案)
  • 出于某种原因,您的 web.config 中有一个 EF 4.1 引用。这是一个新项目,还是要将 EF5 添加到现有的 EF4 项目?
  • 这是一个全新的项目。这就是为什么我认为这是一个配置错误。我安装了 EF5,但似乎所有引用都指向 EF4 ???我认为它没有正确安装...
  • @Gravy - 这是 Visual Studio 2010 还是 2012?
  • 会不会是您不小心安装了 MVC3 而不是 MVC4?

标签: asp.net-mvc entity-framework


【解决方案1】:

在您的视觉工作室solution explorer 中,转到Models 文件夹并打开AccountModels.cs 文件。注释掉下面一行

[Table("UserProfile")]

注释掉后会变成

//[Table("UserProfile")]

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

【讨论】:

  • 完美!正是这个琐碎的细节缺失了。
【解决方案2】:

如果您的项目以 .NET 4.0 为目标,EF 将使用 4.4 版本,即 EF5 版本,其功能范围仅限于 .NET Framework 4 中的核心组件可以支持的内容(即,您将无法获得对枚举或空间类型的支持)。如果您想使用 EF5 附带的所有新功能,您的 MVC 项目需要以 4.5 为目标。

【讨论】:

  • EF5 本身不会降级到 4.1。如果您从 nugget 安装 EF5 包,它包含两个版本的 EntityFramework.dll - 一个适用于 .NET Framework 4.5,另一个适用于 .NET Framework4(显然功能有限),但它们仍然是 EF5。为了澄清 MVC 项目可能会按照这些思路做一些事情(我不知道),但它不是 EF 本身。
  • 我编辑了我的评论以反映 VS2012 报告的内容......当您将 NuGet 中的 EF5 添加到 .NET 4 项目中时,它是实体框架版本 4.4.0.0。我要指出的是 EF5 w/.NET 4.5 和 EF4.4 w/.NET 4.0 之间的区别。
【解决方案3】:

问题解决了……

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

任务完成!!!

:)

【讨论】:

  • 不,你没有解决它,你找到了一个解决方法,现在我有同样的问题,但升级不是我的选择:(
猜你喜欢
  • 1970-01-01
  • 2013-01-22
  • 1970-01-01
  • 1970-01-01
  • 2012-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多