dotnet / dotnet/efcore

Setting global collation for SQLite doesn't set collation for individual columns

Open
#32,051 14 comments 0 reactions 0 assignees View on GitHub
area-migrations area-sqlite customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

When setting `modelBuilder.UseCollation("NOCASE");` in `OnModelCreating()` then text columns do not get this collation.

Suggested as bug in [StackOverflow](https://stackoverflow.com/a/77283802/4353606).

## Steps to reproduce

1. Create a .NET7 console application
2. Add the `Microsoft.EntityFrameworkCore.Sqlite` version 7.0.12
3. Create the following `DbContext` and data model:
```C#
public class MyDbContext : DbContext
{
public DbSet MyTable { get; set; }

public string DbPath { get; }

public MyDbContext()
{
// Copied from "Getting Started with EF Core" https://learn.microsoft.com/en-us/ef/core/get-started/overview/first-app
var folder = Environment.SpecialFolder.LocalApplicationData;
var path = Environment.GetFolderPath(folder);
DbPath = Path.Join(path, "mydb.db");
}

protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite($"Data Source={DbPath}");

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.UseCollation("NOCASE");
base.OnModelCreating(modelBuilder);
}
}

public class MyDbTable
{
public int Id { get; set; }
public string Title { get; set; }
}
```
4. Run `Add-Migration Initial` to create the migration
5. Run `Update-Database" to apply the migration
6. Check the `MyTable.Title` column in the database to see that it does not have the `NOCASE` collation
7. Run `Update-Database -Migration:0` to reset the database
8. Run `Remove-Migration`
9. Replace `modelBuilder.UseCollation("NOCASE");` with `modelBuilder.Entity().Property(t => t.Title).HasColumnType("TEXT COLLATE NOCASE");`
10. Run `Add-Migration Initial`
11. Run `Update-Database`
12. Check the `MyTable.Title` column to see that you can set the `NOCASE` individually

### Provider and version information

EF Core version:
Database provider: Microsoft.EntityFrameworkCore.Sqlite 7.0.12
Target framework: .NET 7.0
Operating system: Windows 11 Pro 22H2 (22621.2428)
IDE: Visual Studio Professional 2022 17.7.5

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.