Skip to content

EnsureCreatedAsync does not allow to set collation #6565

@Ciantic

Description

@Ciantic

I have a database I drop each time I start a debug server, and then recreate it. The problem is that
EnsureCreatedAsync does not allow to define collation.

Here is a really ugly workaround:

await db.Database.EnsureDeletedAsync();
var builder = new SqlConnectionStringBuilder(db.Database.GetDbConnection().ConnectionString) { InitialCatalog = "master" };
builder.Remove("AttachDBFilename");
using (var connection = new SqlServerConnection(new DbContextOptionsBuilder().UseSqlServer(builder.ConnectionString).Options, logger))
{
    connection.Open();
    using (var command = new SqlCommand())
    {
        command.Connection = connection.DbConnection as SqlConnection;
        command.CommandType = System.Data.CommandType.Text;
        command.CommandText = "CREATE DATABASE [mvcApiTest] COLLATE Finnish_Swedish_CI_AS;";
        await command.ExecuteNonQueryAsync();
    }
}
await db.Database.EnsureCreatedAsync();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions