Code First 无法启用迁移

Code First can#39;t enable migrations(Code First 无法启用迁移)
本文介绍了Code First 无法启用迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试启用迁移,但它引发了异常:

I'm trying to enable migrations but it's throwing an exception:

正在检查上下文是否针对现有数据库...System.TypeInitializationException:System.Data.Entity.Migrations.DbMigrationsConfiguration`1"的类型初始化程序引发了异常.---> System.TypeInitializationException:System.Data.Entity.Internal.AppConfig"的类型初始化程序引发了异常.---> System.Configuration.ConfigurationErrorsException:配置系统初始化失败---> System.Configuration.ConfigurationErrorsException:必须在section"标签上指定name"属性.

Checking if the context targets an existing database... System.TypeInitializationException: The type initializer for 'System.Data.Entity.Migrations.DbMigrationsConfiguration`1' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception. ---> System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: The 'name' attribute must be specified on the 'section' tag.

我假设 App.config 文件设置不正确(它是在我添加 EF 包时自动设置的).我所做的只是添加连接字符串:

I'm assuming that the App.config file is not correctly set up (it was automatically set up when I added EF package). All I did was add the connection string:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>

  <section Name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
 <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>

 <connectionStrings>
   <add Name="MyContext" connectionString="data source=MYSERVER;initial catalog=CodeFirstTest;user id=***;password=***;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
 </connectionStrings>

 <entityFramework>
   <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
     <parameters>
       <parameter value="v11.0" />
     </parameters>
   </defaultConnectionFactory>
   <providers>
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
   </providers>
 </entityFramework>
</configuration>

我使用的是 SQL Server 2008 R2.

I'm using SQL Server 2008 R2.

因为我确实有一个连接字符串,所以我认为我不需要默认连接工厂.我对么?(注意:即使没有这部分,我仍然会遇到同样的异常)

As I do have a connection string, I don't believe I need the defaultconnectionfactory. Am I correct? (Note: Even without this section I'm still getting the same exception)

我还缺少什么?

推荐答案

当我的 <connectionString/> 条目位于 Web.config 的顶部时,我遇到了同样的问题,紧接着<配置>.然后我试着把它移到 </configuration> 之前,它成功了.

I had the same issue when my <connectionString /> entry is on top part of the Web.config, Right after <configuration>. Then I tried moving it right before </configuration>, and it worked.

这篇关于Code First 无法启用迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)