14

I'm using Entity Framework CTP5 and Code First. I need to change the Collation for a specific column in SQL Server. I believe the default collation is SQL_Latin1_General_CP1_CI_AS, but I need to change this one column colllation to SQL_Latin1_General_CP1_CS_AS (Case Sensitive).

Is there a way to use ModelBuilder in Code First to change a specific column collation?

BarDev

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
Mike Barlow - BarDev
  • 11,087
  • 17
  • 62
  • 83

2 Answers2

14

Model builder doesn't allow this but you can create custom database initializer and execute ALTER TABLE command. The example will be the same as this one creating custom index.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • 5
    Perfect I did something like this and it seemed to work: context.Database.SqlCommand("ALTER TABLE MyTable ALTER COLUMN MyColumn VARCHAR(50) COLLATE SQL_Latin1_General_CP1_CS_AS NULL"); – Mike Barlow - BarDev Mar 12 '11 at 22:36
3

Some years later:

modelBuilder.Entity<Order>().Property(c => c.Name).UseCollation("SQL_Latin1_General_CP1_CS_AS");
Tod
  • 2,070
  • 21
  • 27
  • 2
    This answer is dependent upon your EF version. OP mentions EF CTP5 for which `UseCollation` does not exist. The method appears to exist in (at least modern versions of) EF Core. – Jasel Nov 01 '22 at 18:09