I'm trying to set up a test db for Unit Testing, as such I added a second connection string called testing and am trying to perform
dotnet ef database update --context ApplicationDbContext --connection "Testing"
However, no matter what connection string I specify, I always get an error:
Format of the initialization string does not conform to specification starting at index 0.
It's weird because my application will run perfectly fine using both connection strings. Its only when I try to migrate or update my databases that I get an issue. In fact, I can still migrate and update without specifying a connection string, but it will only update my DefaultConnection
database.
Take a look at this, I added some logging to my DbContext
to confirm that the connection strings are the same.
public ApplicationDbContext(
DbContextOptions options,
IOptions<OperationalStoreOptions> operationalStoreOptions) : base(options, operationalStoreOptions)
{
Console.WriteLine(options.FindExtension<SqlServerOptionsExtension>().ConnectionString);
}
And you can see in the console the connection strings are the same yet I only get an error when I specify the connection string.
C:\Projects\RateMyManagementWASM\src\Server>dotnet ef database update
Build started...
Build succeeded.
Server=(localdb)\mssqllocaldb;Database=RateMyManagementWASMLocalDb;Trusted_Connection=True;MultipleActiveResultSets=truewarn: Microsoft.EntityFrameworkCore.Model.Validation[10400]
Sensitive data logging is enabled. Log entries and exception messages may include sensitive application data; this mode should only be enabled during development.
Done.
C:\Projects\RateMyManagementWASM\src\Server>dotnet ef database update 0 --connection "DefaultConnection"
Build started...
Build succeeded.
Server=(localdb)\mssqllocaldb;Database=RateMyManagementWASMLocalDb;Trusted_Connection=True;MultipleActiveResultSets=truewarn: Microsoft.EntityFrameworkCore.Model.Validation[10400]
Sensitive data logging is enabled. Log entries and exception messages may include sensitive application data; this mode should only be enabled during development.
System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.
at Microsoft.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue)
at Microsoft.Data.Common.DbConnectionOptions.ParseInternal(Dictionary`2 parsetable, String connectionString, Boolean buildChain, Dictionary`2 synonyms, Boolean firstKey)
at Microsoft.Data.Common.DbConnectionOptions..ctor(String connectionString, Dictionary`2 synonyms)
at Microsoft.Data.SqlClient.SqlConnectionString..ctor(String connectionString)
at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous)
at Microsoft.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions)
at Microsoft.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key)
at Microsoft.Data.SqlClient.SqlConnection.set_ConnectionString(String value)
at Microsoft.Data.SqlClient.SqlConnection..ctor(String connectionString)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.CreateDbConnection()
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnection()
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.<>c__DisplayClass18_0.<Exists>b__0(DateTime giveUp)
at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass12_0`2.<Execute>b__0(DbContext _, TState s)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation, Func`2 verifySucceeded)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists(Boolean retryOnNotExists) at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.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 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 have tried editing my connection string by removing the Trusted Connection
and MultipleActiveResults
sections but it still doesn't work. Nor when I specifically specify --context
Edit: Jeremy Lakemans answer solved my problem. Apparently you have to specify the full connection string when doing dotnet ef database update
I was confused because some other commands allow you to just specify the connection string name.
CodePudding user response:
I created a database using MySQL you can also try it by creating database through MySQL. You can add migrations using the command called add-migrations in Nudget Package Manager console. If it not work check your DbContext class file.
CodePudding user response:
If your unit test project and unit test dbcontext different folder you have to navigate to unit test project or unit test dbcontext folder than you can create
You can check out: https://medium.com/@princeofficial88/how-to-create-ef-core-migrations-in-different-assembly-and-under-a-specific-folder-in-asp-net-core-9269b91de74c