Skip to content

Ability to specify database collation #6577

Description

@valamelkor

There is no way to do this at the moment. Database is created with default settings. It is not an issue on most cases, but there is a plenty of specific ones. For example, I have different collation on production server db, then on development machine.

After some research I've come up with workaround. Pretty ugly so far, but at least working

In Migrations\00000000000000_CreateIdentitySchema.cs add

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.Sql($"DECLARE @dbname sysname = db_name(); " +
    "EXEC('ALTER DATABASE [' + @dbname + '] SET SINGLE_USER WITH ROLLBACK IMMEDIATE');" +
    "EXEC('ALTER DATABASE [' + @dbname + '] COLLATE Cyrillic_General_CI_AS');" +
    "EXEC('ALTER DATABASE [' + @dbname + '] SET MULTI_USER') ", suppressTransaction: true);
    ...
}

It is ok. But the connection is reset after execution of this first migration so I have to run in separately.
I've come up with the following script (resetdb.cmd):

dotnet ef database update 00000000000000_CreateIdentitySchema
dotnet ef database update

So, there should be a proper way to do such things.

See also #6565

Activity

  1. rowanmiller commented on Sep 23, 2016

    @rowanmiller
    Contributor

    We agree this would be useful. It's lower priority than the other features we are working on now, so moving to the backlog to look at in a future release.

  2. added this to the Backlog milestone on Sep 23, 2016
  3. Ciantic commented on Sep 24, 2016

    @Ciantic

    EnsureCreatedAsync there, now this item can be found with GitHub search

  4. seriouz commented on Dec 1, 2016

    @seriouz

    I have a database with some columns collations set to be case sensitive. Now EF build its query with a temp table, but this temp table does not have the collation on the column so on the insert the database server throws an exception.
    Until there is a work-around i can not use ef because the regarding columns contain (case sensitive) guids.

  5. divega commented on Dec 1, 2016

    @divega
    Contributor

    @seriouz thanks for reporting this. I have created a new issue at #7172. Please add more relevant details there.

  6. javiercampos commented on Jan 19, 2018

    @javiercampos

    Has anyone taken a look into this since Dec 2016? I'm using a different (albeit similar) workaround than the OP so I don't have to apply migrations independently (I just open a connection on the Up method and send the SQL directly) but it's definitely pretty ugly:

    [DbContext(typeof(MyDbContext))]
    [Migration("00000000000000_SetupDatabase")]
    public class SetupDatabase : Migration
    {
    	protected override void Up(MigrationBuilder migrationBuilder)
    	{
    		// HACK: there must be a better way
    		using (var connection = new SqlConnection(MyContextOptions.ConnectionString))
    		{
    			connection.Open();
    			using (var command =
    				new SqlCommand(
    					$"ALTER DATABASE [{MyDatabaseName}] COLLATE {MyDefaultCollation}",
    					connection))
    				command.ExecuteNonQuery();
    			SqlConnection.ClearAllPools();
    			connection.Close();
    		}
    	}
    }

    By chance (and I say by chance because as I understand, the MigrationBuilder only builds up the instructions that will be executed later and this could be done at any point in time) when the Up on this first migration is called, the database is already created but no tables have been created yet.

    Still, having native support on the SQL Server provider for this would be great... something like:

    migrationBuilder.AlterDatabase().Annotation("COLLATE", "WhateverCollation_CI_AI");

    Should work

  7. murilosdv commented on Dec 8, 2018

    @murilosdv

    Do we have an update on this? I'll try this workaround but it would be great to have something configurable to do so.

    Something more stylish as

    modelBuilder.Collation("Whatever_CI_AI")

    at least something that disables/enables CI/AI.

    modelBuilder.IsCollationSensitive(false) or something.

  8. RedDeathGitHub commented on Jan 9, 2019

    @RedDeathGitHub

    Issue is open since 2016, any plans to fix this?

  9. ajcvickers commented on Jan 9, 2019

    @ajcvickers
    Contributor

    @RedDeathGitHub This issue is in the Backlog milestone. This means that it is not going to happen for the 3.0 release. We will re-assess the backlog following the 3.0 release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources.

  10. RedDeathGitHub commented on Jan 9, 2019

    @RedDeathGitHub

    @ajcvickers Understood - thank you for the info.

  11. murilosdv commented on Jan 9, 2019

    @murilosdv

    Well, I created an extension using some examples I found and I've come up with this.

        public static class MigrationBuilderExtensions
        {
            public static void SetDatabaseCollationToInsensitive(this MigrationBuilder builder)
            {
                builder.Sql("DECLARE @dbname sysname = db_name(); " +
                            "EXEC('ALTER DATABASE [' + @dbname + '] COLLATE SQL_Latin1_General_CP1_CI_AI');", true);
            }
        }
    

    And after creating your first migration just add the information on the top.

    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.SetDatabaseCollationToInsensitive();
    }
    

    It's working by just using Update-Database, works for me for now.

  12. RedDeathGitHub commented on Jan 10, 2019

    @RedDeathGitHub

    Before I was creating the Db manually earlier, but I run into "interesting" issue where just after creation - the Db did not accept connections yet. So migrations would try to create an existing Db.
    Mostly an issue with testing and local environments but still a nasty one.

    This looks interesting, I'll try it, thanks :).

  13. smitpatel commented on Jan 10, 2019

    @smitpatel
    Contributor

    @RedDeathGitHub - That is SqlServer behavior. After creating brand new database, it does not allow you to connect to it immediately. (Log in failed for user, which EF core uses as clue to non-existent database). We have also seen the same issue in our tests and have wait time and retry logic for that.

  14. 9 remaining items

  15. changed the title [-]Ability to specify collation and other db params upon creation[/-] [+]Ability to specify database collation[/+] on Apr 11, 2020
  16. roji commented on Apr 11, 2020

    @roji
    Member

    Note: am using this issue to track database collation (both at creation time and later alteration). For other database-creation settings, please open separate issues for the specific feature you're missing.

  17. added a commit that references this issue on Apr 11, 2020
    55bd7e2
  18. added a commit that references this issue on Apr 11, 2020
    d2b8650
  19. added a commit that references this issue on Apr 14, 2020
    64695e3
  20. modified the milestones: 5.0.0, on Apr 20, 2020
  21. modified the milestones: , 5.0.0 on Nov 7, 2020
  22. added theissue type on Jun 15, 2025
  23. marked Database Creation Parameters #16941 as a duplicate of this issue on Jun 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions