Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# + MySql + Code First: Format of the initialization string does not conform to specification starting at index 0

I have created a Library Project to connect with MySQL DB.

  1. Including MySql.Data, MySql.Data.Entity.EF6 by Nuget => OK.
  2. Installed EntityFramework by Nuget => OK
  3. I created a MyContext.cs:
namespace Project_Core.DAL.Context
{
    [DbConfigurationType(typeof(MySqlEFConfiguration))]
    public class MyContext: DbContext
    {
        public MyContext()
            : base("myDbContext")
        {

        }

        public DbSet<Staff> Staffs { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }
}
  1. I edited the App.config:

<connectionStrings> <add name="myDbContext" providerName="MySql.Data.MySqlClient" connectionString="Data Source=localhost; port=3306; Initial Catalog=dbtest; uid=root; pwd=1234;" /> </connectionStrings>

  1. I opened "Package Manager Console" and wrote:

Enable-Migrations -Force

but I got these errors:

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 MySql.Data.MySqlClient.MySqlConnectionStringBuilder..ctor(String connStr)
   at MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString(String value)
   at MySql.Data.MySqlClient.MySqlConnection..ctor(String connectionString)
   at MySql.Data.Entity.MySqlConnectionFactory.CreateConnection(String connectionString)
   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)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext)
   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.

Help me solve it, thank you!

like image 721
Hoan Tran Avatar asked Dec 03 '25 09:12

Hoan Tran


2 Answers

Solved

  • I have tried to change base("myDbContext") to base("name=myDbContext"), but it throw the errer: "No connection string named 'myDbContext' could be found in the application config file."

  • I have been searching and decided copying the connectionString to base: base("server=localhost;user id=root;password=1234;persistsecurityinfo=True;database=dbtest") and it worked.

like image 96
Hoan Tran Avatar answered Dec 05 '25 23:12

Hoan Tran


Hey so the real fix(not the hack that you are doing here)...

It wont tell you this but the real reason the standard base("{DBConnectionName}") is failing is because of a permissions issue on the folder to access the web.config since the migrations are actually run under a different user.

I had this exact problem so what i did is goto the folder where the web.config is housed, 1) right click and goto properties 2) Goto security 3) Add User, Everyone 4)Tick Full Control 5) Goto Advanced 6) Tick the box that says Replace child object permissions with these 7) hit apply 8) rerun the Add-Migration or Enable-Migration and it will work fine.

Hope this helps someone!

like image 32
willthiswork89 Avatar answered Dec 05 '25 22:12

willthiswork89



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!