Entity Framework - Format of the initialization st

2019-06-25 04:11发布

问题:

Here's the exception,

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)

My connection string is,

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

I have no idea whats going on. What about my connection string isnt working?

!!Update!!

Interestingly - I found that when I use web deploy a new Connection String and Entity Framework > Default Connection Factory > parameter gets added... I know right? What?!

The result of my published "production ready" web.config looks like this:

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

Pretty interesting huh?

Now, when published the web site throws the "format of connection string error".

I think what I want is to remove the need automated addition of the new connection string & context and have be exactly what I've put in the web config.

Whats worst is that this so-called "feature" breaks the use of web.config transforms. In fact, I've come to enjoy using the new "Preview Transform" feature. However, even the preview does show these changes.

What's going on here?

Can I remove that?

回答1:

If you add this to Global.asax, you should have an idea what is wrong with the string..

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

If you see any token place holders in it you should have a better idea what to do.

Also check the permissions on the SQL server for your user..



回答2:

I followed all suggestions, nothing worked. But I found this line created the error: Database.EnsureCreated()

After I removed the EnsureCreated method I was able to create my first migration.