Search code examples
c#sqliteentity-frameworkconnection-string

Getting error with connection string when calling update-database in EF code first, but the connection string is correct (checked with other tools)


I have a test application that uses EF and it is using Context Factory to create context for use with migration.

I can call add migration and it generates suitable migration in my project, but when I call update-database, I get an error.

The context factory is as follow

public class ContextFactory: IDesignTimeDbContextFactory<DBContext>
{
    public DBContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<DBContext>();
        
        optionsBuilder.UseSqlite("Data Source=C:\\tmp\\sqlite.db; Version=3;");
       
        return new DBContext(optionsBuilder.Options);
    }
}

and the error that I am getting is as follows:

PM> Update-Database
Build started...
Build succeeded.
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(Dictionary`2 parsetable, String connectionString, Boolean buildChain, Dictionary`2 synonyms, Boolean firstKey)
   at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Dictionary`2 synonyms, Boolean useOdbcRules)
   at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)
   at Microsoft.Data.Sqlite.SqliteConnectionStringBuilder..ctor(String connectionString)
   at Microsoft.Data.Sqlite.SqliteConnectionFactory.GetPoolGroup(String connectionString)
   at Microsoft.Data.Sqlite.SqliteConnection.set_ConnectionString(String value)
   at Microsoft.Data.Sqlite.SqliteConnection..ctor(String connectionString)
   at Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteRelationalConnection.CreateDbConnection()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnection()
   at Microsoft.EntityFrameworkCore.Diagnostics.RelationalLoggerExtensions.MigrateUsingConnection(IDiagnosticsLogger`1 diagnostics, IMigrator migrator, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Format of the initialization string does not conform to specification starting at index 0.

I checked with the SQLite server compact toolbox and confirmed the connection string is valid.

what is the problem and how can I solve it? How can I get better error data so I can fix it?


Solution

  • I applied your code and I get couple errors not related to yours

    connection string keyword version is not supported

    Then I deleted the keyword Version=3; from connection string then following error appear

    unable to open database file

    Then I create folder c:\tmp and finally it works just fine

    I think you change the connection string somewhere, maybe in the Dependency injection, or by override OnConfiguring

    Temporary, add the following OnConfiguring Method to your DBContext to Make sure that the problem is coming really from the connection string!

     protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
     {
         optionsBuilder.UseSqlite("Data Source=C:\\tmp\\sqlite.db;");
     }