Skip to content

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

@gthvidsten

Description

@gthvidsten

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

Suggested as bug in StackOverflow.

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:
public class MyDbContext : DbContext
{
    public DbSet<MyDbTable> 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; }
}
  1. Run Add-Migration Initial to create the migration
  2. Run `Update-Database" to apply the migration
  3. Check the MyTable.Title column in the database to see that it does not have the NOCASE collation
  4. Run Update-Database -Migration:0 to reset the database
  5. Run Remove-Migration
  6. Replace modelBuilder.UseCollation("NOCASE"); with modelBuilder.Entity<MyDbTable>().Property(t => t.Title).HasColumnType("TEXT COLLATE NOCASE");
  7. Run Add-Migration Initial
  8. Run Update-Database
  9. 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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy