实体框架 - 索引0处的初始化字符串的格式

时间:2013-03-13 22:26:29

标签: c# entity-framework asp.net-mvc-4 web-config

这是例外,

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.SqlClient.SqlConnectionString..ctor(String connectionString)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions)
   at System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key)
   at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value)
   at System.Data.Entity.Internal.LazyInternalConnection.Initialize()
   at System.Data.Entity.Internal.LazyInternalConnection.get_Connection()
   at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo)
   at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbConnectionInfo connectionInfo)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext)
   at System.Data.Entity.MigrateDatabaseToLatestVersion`2.InitializeDatabase(TContext context)
   at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
   at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
   at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.OrderByDescending[TSource,TKey](IQueryable`1 source, Expression`1 keySelector)
   at Article.GetAll() in Article.cs:line 43
   at ASP._Page_Views_Home_Index_cshtml.Execute() in Index.cshtml:line 9
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.StartPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult)

我的连接字符串是,

  <connectionStrings>
    <add name="MyContext" connectionString="Persist Security Info=false;Integrated Security=false;Connection Timeout=4;Initial Catalog=<nice>;User ID=<nice>;pwd=<nice>;server=<nice>" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="Data Source=<nice>;Initial Catalog=Master;Persist Security Info=False;User ID=<nice>;Password=<nice>;MultipleActiveResultSets=True;Application Name=EntityFramework" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>

我不知道最近发生了什么。我的连接字符串怎么办?

!!更新!!

有趣的是 - 我发现当我使用Web部署新的连接字符串和实体框架时&gt;默认连接工厂&gt;参数被添加......我知道对不对?什么?!

我发布的“生产就绪”web.config的结果如下所示:

<connectionStrings>
    <add name="MyContext" connectionString="Data Source=<nice>;Initial Catalog=<nice>;Persist Security Info=False;User ID=<nice>;Password=<nice>; MultipleActiveResultSets=True;Application Name=EntityFramework" providerName="System.Data.SqlClient" />
    <add name="MyContext_DatabasePublish" connectionString="Data Source=<nice>;Initial Catalog=<nice>;Persist Security Info=False;User ID=<nice>;Password=; MultipleActiveResultSets=True;Application Name=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
        <parameters>
            <parameter value="DatabasePublish" />
        </parameters>
    </defaultConnectionFactory>
    <contexts>
        <context type="MyContextDBContext, Models">
            <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[MyContextDBContext, Models], [Models.Migrations.Configuration, MyWebsite.Models]], EntityFramework, PublicKeyToken=<nice>">
                <parameters>
                    <parameter value="DatabasePublish" />
                </parameters>
            </databaseInitializer>
        </context>
    </contexts>
</entityFramework>

非常有趣吗?

现在,发布时网站会抛出“连接字符串错误的格式”。

我认为我想要的是不再需要自动添加新的连接字符串&amp;上下文,并且正是我在web配置中添加的内容。

最糟糕的是,这个所谓的“功能”打破了web.config转换的使用。事实上,我开始喜欢使用新的“预览变换”功能。但是,即使预览确实显示了这些变化。

这里发生了什么?

我可以删除吗?

2 个答案:

答案 0 :(得分:0)

如果将其添加到Global.asax,您应该知道字符串有什么问题..

throw new Exception(ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString);

如果您看到任何令牌占位符,您应该更好地了解该怎么做。

同时检查用户的SQL服务器权限..

答案 1 :(得分:0)

我遵循了所有建议,没有任何效果。但我发现这一行造成了错误: Database.EnsureCreated()

删除EnsureCreated方法后,我能够创建第一次迁移。