Why won't EF Core migration tools add column collation?

35 views Asked by At

I'm trying to create a table in my SQL Server database with a column with a collation different from what I've used for the database. I'm doing this using .NET 8 and EF Core 8.0.1, code first modeling.

The problem is that I cannot seem to get the generated migration or snapshot to specify the collation for the column.

Here is the relevant code.

Configuration:

public class VendorConfiguration : BaseEntityConfiguration<Vendor>
{
    public new void Configure(EntityTypeBuilder<Vendor> builder)
    {
        base.Configure(builder);

        builder
            .Property(x => x.Name_CS) // string property
            .IsRequired() 
            .UseCollation("SQL_Latin1_General_CP1_CS_AS");
            // Database collation is "Sql_Latin1_General_CP1_CI_AS"

        builder
            .HasIndex(x => x.Name_CS)
            .IsUnique();

        builder
            .Property(x => x.Name_CI)
            .IsRequired();

        builder
            .HasIndex(x => x.Name_CI)
            .IsUnique();
    }
}

Context:

public class MyContext : DbContext
{
    public DbSet<Vendor> Vendors { get; set; }

    private readonly string connectionString;

    public MyContext (IOptions<MyConfiguration> configuration, DbContextOptions<MyContext > options) : base (options)
    {
        connectionString = configuration.Value.ConnectionStrings[nameof(ConnectionString)];
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(connectionString);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder
            .UseCollation("SQL_Latin1_General_CP1_CI_AS")
            .ApplyConfiguration(new VendorConfiguration());
    }
}

Migration generated:

    migrationBuilder.CreateTable(
                name: "Vendors",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name_CS = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    Name_CI = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    Uid = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    TimeStamp = table.Column<byte[]>(type: "rowversion", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Vendors", x => x.Id);
                    table.UniqueConstraint("AK_Vendors_Uid", x => x.Uid)
                        .Annotation("SqlServer:Clustered", false);
                });

EDIT: It seems that the use of column collations only applies when accessing the column outside of a configuration object. The below in the OnModelCreating method of the context, caused the custom collation to appear in the generated migration code:

modelBuilder
    .Entity<Vendor>()
    .Property(x => x.Name_CS)
    .UseCollation("SQL_Latin1_General_CP1_CS_AS");
0

There are 0 answers