Code First无法启用迁移 [英] Code First can't enable migrations
问题描述
我正在尝试启用迁移,但会引发异常:
检查上下文是否定位到现有的数据库...
System.TypeInitializationException:'System.Data.Entity.Migrations.DbMigrationsConfiguration`1'的类型初始化程序抛出异常。 ---> System.TypeInitializationException:'System.Data.Entity.Internal.AppConfig'的类型初始化器抛出异常。 ---> System.Configuration.ConfigurationErrorsException:配置系统无法初始化---> System.Configuration.ConfigurationErrorsException:'name'属性必须在'section'标签上指定。
< blockquote>
我假设App.config文件没有正确设置(当我添加EF包时自动设置)。我所做的是添加连接字符串:
<?xml version =1.0encoding =utf-8? >
< configuration>
< configSections>
< section Name =entityFrameworktype =System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,EntityFramework,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089requirePermission =假/>
<! - 有关实体框架配置的更多信息,请访问http://go.microsoft.com/fwlink/?LinkID=237468 - >< / configSections>
< connectionStrings>
< add Name =MyContextconnectionString =data source = MYSERVER; initial catalog = CodeFirstTest; user id = ***; password = ***; MultipleActiveResultSets = True; App = EntityFrameworkproviderName = .Data.SqlClient/>
< / connectionStrings>
< entityFramework>
< defaultConnectionFactory type =System.Data.Entity.Infrastructure.LocalDbConnectionFactory,EntityFramework>
< parameters>
< parameter value =v11.0/>
< / parameters>
< / defaultConnectionFactory>
< providers>
< provider invariantName =System.Data.SqlClienttype =System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer/>
< / providers>
< / entityFramework>
< / configuration>
我使用SQL Server 2008 R2。
因为我有一个连接字符串,我不相信我需要defaultconnectionfactory。我对么? (注意:即使没有这个部分我仍然得到同样的例外)
我还缺少什么?
解决方案当我的
< connectionString />
条目位于Web.config的顶部时,我有同样的问题在< configuration>
之后。然后我尝试在< / configuration>
之前移动它,它的工作。I'm trying to enable migrations but it's throwing an exception:
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.
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>
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)
What else am I missing?
解决方案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无法启用迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!