You only get this if you set the COLLATION
of the column (or the table, or the bank) before. There are a few ways to do this.
One of these is defining a base initializer , like this:
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);
}
}
E:
public class MeuContexto: DbContext
{
public MeuContexto() : base("DefaultConnection", throwIfV1Schema: false)
{
Database.SetInitializer(new CollationInitializer<MeuContexto>());
if(!Database.Exists())
{
Database.Initialize(true);
}
}
}
Another is setting up a migration interceptor to set COLLATION
to the time the database 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) { }
}