Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
divega opened this issue Dec 1, 2016 · 10 comments
Open

Comments

@divega
Copy link
Contributor

divega commented Dec 1, 2016

(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

@seriouz
Copy link

seriouz commented Dec 1, 2016

@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.

@divega
Copy link
Contributor Author

divega commented Dec 1, 2016

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

@rowanmiller rowanmiller added this to the 1.2.0 milestone Dec 7, 2016
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Apr 19, 2017
@AndriySvyryd
Copy link
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.

@AndriySvyryd AndriySvyryd removed this from the 2.0.0 milestone Jun 9, 2017
@AndriySvyryd
Copy link
Member

AndriySvyryd commented Jun 9, 2017

@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");

@ajcvickers ajcvickers added this to the Backlog milestone Jun 12, 2017
@ajcvickers
Copy link
Member

ajcvickers commented Jun 12, 2017

Should at least do the R.E. work. We may need to look at the whole update pipeline "experience" as well...

@ajcvickers
Copy link
Member

Also see high-level collations issue #19866

@roji
Copy link
Member

roji commented Mar 28, 2022

Note that since #27372 we no longer use table variables by default - we use simple OUTPUT, rather than OUTPUT INTO, unless the user explicitly tells us that the table has triggers.

david-wimmer added a commit to david-wimmer/efcore that referenced this issue Nov 9, 2022
- Add property annotation RelationalAnnotationNames.Collation to runtime model
- Consider column collation annotation when creating DELCARE TABLE statement
- Fixed unit test

Fixes dotnet#7172
@david-wimmer
Copy link

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

@angularsen
Copy link

angularsen commented Dec 9, 2022

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.

@AndriySvyryd
Copy link
Member

Depends on #32546 and #29620

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants