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

本文关键字:字符串 格式 初始化 0处 框架 索引 实体 | 更新日期: 2023-09-27 18:04:00

有一个例外,

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部署一个新的连接字符串和实体框架>默认连接工厂>参数被添加…我知道,对吧?什么? !

我发布的"生产就绪"web的结果。配置如下:

<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>

很有趣吧?

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

我想我想要的是消除自动添加新连接字符串&上下文和已经完全是我已经在web配置。

最糟糕的是这个所谓的"功能"破坏了web的使用。配置转换。事实上,我已经开始喜欢使用新的"预览变换"功能。然而,即使是预览版也显示了这些变化。

这是怎么回事?

我可以删除吗?

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

如果您将此添加到全局。现在,您应该知道字符串..

有什么问题了。
throw new Exception(ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString);

如果您在其中看到任何令牌占位符,您应该更好地知道该怎么做。

还要检查用户在SQL服务器上的权限。

我遵循了所有的建议,但都不起作用。但我发现这一行产生了错误:Database.EnsureCreated ()

在我删除了EnsureCreated方法之后,我能够创建我的第一个迁移。