brstkr
brstkr

Reputation: 1623

Entity Framework Core How To Solve ..Index 0-Error?

I am getting following error prompt from my Packet-Manager Console.

PM> Update-Database -Context SQLiteTestDbContext
Build started...
Build succeeded.
Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 3.1.4 initialized 'SQLiteTestDbContext' using provider 'Microsoft.EntityFrameworkCore.Sqlite' with options: None
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.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, 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 don't know where the problem is. My connection string is stored in a appsettings.json in the project path.

appsettings.json:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true",
    "SQLiteTestConnection": "Data Source=../DB-SQLite/Tests/TestDB.db"
  }
}

What is the problem there? The Add-Migration has worked for me but the next part with Update-Database won't work.

Upvotes: 0

Views: 1758

Answers (2)

Arman Brki
Arman Brki

Reputation: 11

I solve my problem by editing ConfigureService Method in the Startup class. the problem will be solved when you actually get connection string from Configuration property. so the final shape of adding dbContext into Application Services in ConfigureService method should be like:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<yourDBContext>(
        option => option.UseSqlServer(Configuration.GetConnectionString("SomeConnection"))
    );
}

Upvotes: 1

brstkr
brstkr

Reputation: 1623

Ok I found my problem. My connection string was not correct. Also I tried with the ConfigurationManager to get the ConenctionString which was stored in an appsettings.json but this won't work (its only for .config I guess).

Upvotes: 1

Related Questions