Answer the question
In order to leave comments, you need to log in
How to get rid of "Both an existing DbConnection and a connect" error when testing an application in xUnit?
https://gist.github.com/Taifunov/5b50081e2a86a0fc6... - my dbContext
https://gist.github.com/Taifunov/dcf99ec62140e071d... - my test class
I get this error:
System.InvalidOperationException : Both an existing DbConnection and a connection string have been configured. When an existing DbConnection is used the connection string must be set on that connection.
Answer the question
In order to leave comments, you need to log in
Well, in the example https://docs.microsoft.com/en-us/ef/core/miscellan... , from which you took the code in English, but in white it is written https://docs.microsoft.com/en-us /ef/core/miscellan...
Nevertheless, you are setting up two database providers in the context.
Here we configure to use a file instance:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Filename=dbTest.db", options =>
{
options.MigrationsAssembly(Assembly.GetExecutingAssembly().FullName);
});
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlite("Filename=dbTest.db");
}
base.OnConfiguring(optionsBuilder);
}
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<MyDbContext>()
.UseSqlite(connection)
.Options;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlite("Filename=dbTest.db", options =>
{
options.MigrationsAssembly(Assembly.GetExecutingAssembly().FullName);
});
}
base.OnConfiguring(optionsBuilder);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question