Skip to content

Batching through a table variable may not work for non-default collations  #7172

Description

@divega

(copied from @seriouz's comment at #6577 (comment))

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.

I think @seriouz meant to say SaveChanges() fails, as opposed to querying data.

A possible workaround is to disable batching in either the OnConfiguring() method on the DbContext class or on the AddDbContext() method in Startup:

    optionsBuilder => optionsBuilder
        .UseSqlServer(connectionString)
        .MaxBatchSize(1);

cc @AndriySvyryd as FYI

Activity

  1. seriouz commented on Dec 1, 2016

    @seriouz

    @divega you are right. I create multiple parent and child entities. Which reference each other with case-sensitive guid keys. (something like Pk8hUi....) All of them are added by _dbContext.Add(). And then saved in one chuck with _dbContext.SaveChanges().
    In the debug output i can see this line from the query generated by ef:
    DECLARE @inserted0 TABLE ([FieldGuid] nvarchar(22), [_Position] [int]); the ef query builder misses to check the collation of the underlying column which has a COLLATE SQL_Latin1_General_CP1_CS_AS resulting in a collation missmatch thrown by the mssql server.

  2. divega commented on Dec 1, 2016

    @divega
    ContributorAuthor

    @seriouz thanks for confirming. Please let me know if the suggested workaround helps.

  3. added this to the milestone on Dec 7, 2016
  4. modified the milestones: , 2.0.0 on Apr 19, 2017
  5. AndriySvyryd commented on Jun 9, 2017

    @AndriySvyryd
    Member

    We would need to add the ability of specifying collation for a given column to be able to use it in the update pipeline.

  6. removed this from the 2.0.0 milestone on Jun 9, 2017
  7. AndriySvyryd commented on Jun 9, 2017

    @AndriySvyryd
    Member

    @seriouz As a workaround you can specify the collation as part of the store type:

    .Property(e => e.FieldGuid).HasColumnType("nvarchar(22) COLLATE SQL_Latin1_General_CP1_CS_AS");
  8. added this to the Backlog milestone on Jun 12, 2017
  9. 10 remaining items

  10. added a commit that references this issue on Nov 9, 2022
    818447e
  11. david-wimmer commented on Nov 9, 2022

    @david-wimmer

    I created a PR #29518 with a proposed fix to this issue.

  12. angularsen commented on Dec 9, 2022

    @angularsen

    Workaround: Save one parent entity at a time. It correctly saves related data in its properties.

    I didn't see this workaround mentioned anywhere, but it was very helpful for us.

  13. AndriySvyryd commented on Feb 5, 2024

    @AndriySvyryd
    Member

    Depends on #32546 and #29620

  14. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions