Entity Framework - initialization string format with index 0

Here is 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:

  <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 what is going on. How about my connection string not working?

!! Update !!

Interestingly, I found that when using web deployment, a new Connection String parameter and Entity Framework> Default Connection Factory> are added ... Do I know correctly? What?!

The result of my published "finished production" web.config is as follows:

 <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 that the published website is displaying a “connection string error format”.

I think I want to remove the need to automatically add a new connection string and context and be what I put in the web configuration.

In the worst case, this so-called “function” violates the use of web.config transformations. In fact, I liked using the new "Preview Transform" feature. However, even a preview shows these changes.

What's going on here?

Is it possible to remove this?

+4
source share
2 answers

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

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

If you see token holders in it, you should better understand what to do.

Also check permissions on the SQL server for your user.

0
source

I followed all the offers, nothing worked. But I found that this line generated an error: Database.EnsureCreated ()

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

0
source

Source: https://habr.com/ru/post/1469093/


All Articles