asp.net-mvc - 无法检索 'MyService.Entities.Email' 的元数据。 'System.Data.Entity.Internal.AppConfig' 的类型初始值设定项引发异常

标签 asp.net-mvc entity-framework controller metadata asp.net-mvc-4

我正在尝试使用 MVC 4.0 和 EF 4.3.1 的新 Visual Studio 11 Beta。

添加 Controller 时,我收到以下消息“无法检索“MyService.Entities.Email”的元数据。“System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发异常”

重现

我有一个基本的解决方案,其中有一个实体项目 (MyService.Entities) 和 MVC 项目 (MyService.Website)。

在实体项目中,我首先拥有代码的基本框架,其中包含“电子邮件”类和 DbContext 类。这两个类看起来像: 实体数据库

   namespace MyService.Entities
{
    public class EntitiesDb : DbContext
    {
        public EntitiesDb(string nameOrConnectionString)
            : base(nameOrConnectionString)
        { }

        public EntitiesDb()
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // Sets up the defaults for the model builder
            // Removes the metadata being written to the database, This is being depreciated anyhow 
            modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

            // Sets the table names so that they are named as a none plural name 
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            // Creates the database from scratch when it builds
            base.OnModelCreating(modelBuilder);
        }

        public DbSet<Email> Emails { get; set; }
    }
}

电子邮件

namespace MyService.Entities
{
    public class Email
    {
        public Email()
        {
        }

        [Key, DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]
        public int EmailId { get; set; }
        public string Subject { get; set; }
        public string BodyHTML { get; set; }
    }
}

我在 MyService.Website MVC 项目中引用了 MyService.Entities 项目,并设置了连接字符串以适合我的数据库名称

<add name="EntitiesDb" connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=EntitiesDb;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />

当我通过执行以下操作来创建 MVC 脚手架时:

  • 右键单击 Controller 文件夹> 添加> Controller
  • 将 Controller 名称设置为EmailController
  • 使用 Entity Framework 将模板设置为具有读/写操作和 View 的 Controller
  • 将模型类设置为Email(MyService.Entities)
  • 将数据上下文类设置为EntitiesDb (MyService.Entities)
  • 将 View 设置为Razor

它显示一条消息

“无法检索“MyService.Entities.Email”的元数据。“System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发异常”

令人困惑的是,它最初起作用,然后我开始向数据库中添加更多实体和属性,然后它不允许我这样做。我现在已经缩小了规模,而且它仍在这样做。

在查看论坛后我尝试了一些方法:

  • 检查 MVC 项目中的连接字符串是否正确添加
  • 实体项目中的连接字符串
  • 重新安装了 Visual Studio 11 Beta,以防任何更新对其产生影响
  • 检查引用是否匹配(它们都是 EntityFramework 版本4.3.1.0)
  • 删除数据库进行重建(现在我没有;o))

这是 VS11 中的错误吗?我是否在某处缺少对元数据的引用?

非常感谢任何帮助。

编辑: 通过卸载 EntitiesFramework 并重新安装 NuGet 包解决了该问题。

最佳答案

从 webconfig 中删除此部分

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory,    EntityFramework" />
<providers>
 </providers>
</entityFramework>

关于asp.net-mvc - 无法检索 'MyService.Entities.Email' 的元数据。 'System.Data.Entity.Internal.AppConfig' 的类型初始值设定项引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10565266/

相关文章:

asp.net-mvc - Azure OpenID 通过 OWIN 中间件连接导致无限重定向循环

asp.net - 类型 'ASP._Page_index_cshtml' 不继承自 'System.Web.WebPages.WebPage'

asp.net - 在 ASP.NET MVC 中访问服务器上的文件

c# - 使用通用存储库时将 'Type' 特定函数放在哪里

JAVAFX:阻止用户打开新窗口,直到他关闭第一个窗口

php - 在 laravel 中使用原始表达式获取用户的最新条目

c# - 何时使用 var 何时使用强类型?

entity-framework - 使用通用存储库是一种好习惯还是每个实体都应该拥有自己的存储库?

c# - ef6 和级联删除

variables - 在 Zend 框架 2 中将变量设置为 404