You can only do that if you define the COLLATION
column (or table, or bank) before. There are some ways to do this.
One of them is defining a baseline initializer, thus:
public class CollationInitializer : CreateDatabaseIfNotExists<MeuContexto>
{
public override void InitializeDatabase(MeuContextocontext)
{
if(!context.Database.Exists())
{
using (SqlConnection connection = new SqlConnection("DefaultConnection"))
{
connection.Open();
using(SqlCommand command = new SqlCommand(string.Format("CREATE DATABASE {0} COLLATE Latin1_General_CI_AI", "MinhaBaseDeDados"), connection))
{
command.ExecuteNonQuery();
}
}
SqlConnection.ClearAllPools();
}
base.InitializeDatabase(context);
}
}
And:
public class MeuContexto: DbContext
{
public MeuContexto() : base("DefaultConnection", throwIfV1Schema: false)
{
Database.SetInitializer(new CollationInitializer<MeuContexto>());
if(!Database.Exists())
{
Database.Initialize(true);
}
}
}
Another is defining a migration developer to define the COLLATION
at the time the bank is being updated:
public class CreateDatabaseCollationInterceptor : IDbCommandInterceptor
{
private readonly string _collation;
public CreateDatabaseCollationInterceptor(string collation)
{
_collation = collation;
}
public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext) { }
public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
// Apenas para SQL Server
if (Regex.IsMatch(command.CommandText, @"^create database \[.*]$"))
{
command.CommandText += " COLLATE " + _collation;
}
}
public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext) { }
public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext) { }
public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext) { }
public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext) { }
}