-
-
Notifications
You must be signed in to change notification settings - Fork 256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow custom Database-name in MsSqlTestcontainerConfiguration #541
Comments
I should have checked the Pull-Requests before creating an issue... 🤦♂️ |
Yep 😅. Would you like to take a look at that? Any help is more than welcome. |
…nfiguration (creating database using SQL script execution)
You can achieve this by calling Here is some code I use in my unit tests. internal static class TestSqlServerDatabaseFactory
{
private static readonly Lazy<ValueTask<MsSqlContainer>> GetServerInstance = new(CreateServerInstanceAsync);
private static int? Port;
public static async ValueTask<string> CreateDatabaseAndGetConnectionStringAsync()
{
MsSqlContainer serverInstance = await GetServerInstance.Value;
string databaseName = "Test" + Guid.NewGuid().ToString().Replace("-", "");
await serverInstance.ExecScriptAsync($"create database [{databaseName}]");
string connectionString =
serverInstance
.GetConnectionString()
.Replace("Database=master", $"Database={databaseName}");
return connectionString;
}
public static void SetPort(int port)
{
if (Port is not null && Port != port)
Port = port;
}
private static async ValueTask<MsSqlContainer> CreateServerInstanceAsync()
{
var result =
new MsSqlBuilder()
.WithAutoRemove(autoRemove: true)
.WithExposedPort(Port ?? 54465)
.Build();
await result.StartAsync();
return result;
}
} |
Is your feature request related to a problem? Please describe.
I'm trying to switch all our tests to use Testcontainers instead of real databases on a server. However, at least one of our Entity Framework migrations contains code to alter the current database. This does not work, because
MsSqlTestcontainerConfiguration
defaults to themaster
-DB which may not be altered.When I try to set the DB-name in the configuration, a
NotImplementedException
will be thrown.testcontainers-dotnet/src/Testcontainers/Configurations/Modules/Databases/MsSqlTestcontainerConfiguration.cs
Lines 34 to 38 in fff8c08
Describe the solution you'd like
MsSqlTestcontainerConfiguration.Database
should be customizeable.Describe alternatives you've considered
I don't know any alternatives (yet).
Additional context
This is probably the migration that fails when using the
master
-DB:The text was updated successfully, but these errors were encountered: