EnableAutomaticMigrations错误:初始化字符串的格式不符合从索引0开始的规范

时间:2015-03-18 03:03:33

标签: .net entity-framework

我试图在软件包管理器控制台中运行以下命令:

Enable-Migrations -EnableAutomaticMigrations -Force

但我一直得到以下输出:

Checking if the context targets an existing database...
System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.
   at System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue)
   at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey)
   at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules)
   at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)
   at System.Data.SqlClient.SqlConnectionStringBuilder..ctor(String connectionString)
   at System.Data.Entity.Infrastructure.SqlConnectionFactory.CreateConnection(String nameOrConnectionString)
   at System.Data.Entity.Internal.LazyInternalConnection.Initialize()
   at System.Data.Entity.Internal.LazyInternalConnection.get_Connection()
   at System.Data.Entity.Internal.LazyInternalContext.get_Connection()
   at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo, Func`1 resolver)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
   at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldInitialCreate(String language, String rootNamespace)
   at System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Format of the initialization string does not conform to specification starting at index 0

我的app.config文件包含此连接字符串:

<add name="EFDataAccessLayer.My.MySettings.SQLServerConnectionString"
  connectionString="Data Source=CDV-WIN-PC\SQLEXPRESS;Trusted_Connection=True;Database=Test" />

我也试过这个:

<add name="EFDataAccessLayer.My.MySettings.SQLServerConnectionString"
      connectionString="metadata=res://*/Data.Model.csdl|res://*/Data.Model.ssdl|res://*/Data.Model.msl;provider=System.Data.SqlClient;provider connection string=&quot; Data Source=CDV-WIN-PC\SQLEXPRESS;Trusted_Connection=True;Database=Test&quot;" />

我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

即使您在Package Manager Console中选择了正确的DbContext,它仍然会查看当前启动项目的web.config文件,其中连接字符串不存在。< / p>

将包含__get__类的项目设置为启动项目以解决此问题。

答案 1 :(得分:0)

在PM控制台中运行此问题后,我在Web API 2中遇到了同样的问题:

Enable-Migrations -EnableAutomaticMigrations -ConnectionString IdentityConnection -ConnectionProviderName System.Data.SqlClient -Force

我通过将其更改为实际使用ApplicationDbContext中创建的IdentityModels来修复它。

Enable-Migrations -ContextTypeName ApplicationDbContext -EnableAutomaticMigrations -Force

有趣的是,它不仅引用了相同的连接字符串,而且构造函数还包含其他帖子中的某个人所说的可能修复的代码(即throwIfV1Schema: false建议。