You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is a collation issue with SaveChangesAsync when the Merge is used (ValueGeneratedOnAdd).
Code sample
usingMicrosoft.EntityFrameworkCore;usingMicrosoft.Extensions.Logging;awaitusingvarctx=newTestContext();awaitctx.Database.EnsureDeletedAsync();awaitctx.Database.EnsureCreatedAsync();varcount=awaitctx.KeyDefinitionEntities.Where(t =>t.TenantId=="test").CountAsync();Console.WriteLine("Count of records = "+count);vartests=ctx.KeyDefinitionEntities.ToList();for(vari=0;i<50;i++)if(tests.FirstOrDefault(t =>t.Id==i.ToString())!=null)tests.First(t =>t.Id==i.ToString()).TenantId="2";elsectx.KeyDefinitionEntities.Add(newKeyDefinitionEntity{Id=i.ToString(),TenantId="1"});awaitctx.SaveChangesAsync();publicclassTestContext:DbContext{publicDbSet<KeyDefinitionEntity>KeyDefinitionEntities{get;set;}protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder){optionsBuilder.UseSqlServer(@"Server=localhost; Database=test; User=...;Password=...").LogTo(Console.WriteLine,LogLevel.Information).EnableSensitiveDataLogging();}protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){modelBuilder.UseCollation("SQL_Latin1_General_CP1_CS_AS");modelBuilder.Entity<KeyDefinitionEntity>().Property(t =>t.Id).UseCollation("SQL_Latin1_General_CP1_CI_AS");// Default value will force Merge statementmodelBuilder.Entity<KeyDefinitionEntity>().Property(t =>t.IsFolder).HasDefaultValue(true).ValueGeneratedOnAdd();modelBuilder.Entity<KeyDefinitionEntity>().HasData(newKeyDefinitionEntity{Id="1",TenantId="1"});}}publicclassKeyDefinitionEntity{publicstring?TenantId{get;set;}publicstringId{get;set;}publicboolIsFolder{get;set;}}
Error
SELECT [t].[IsFolder] FROM [KeyDefinitionEntities] t
INNER JOIN @inserted1 i ON ([t].[Id] = [i].[Id])
ORDER BY [i].[_Position];
fail: 3/31/2022 15:54:33.785 CoreEventId.SaveChangesFailed[10000] (Microsoft.EntityFrameworkCore.Update)
An exception occurred in the database while saving changes for context type 'TestContext'.
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
---> Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CS_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.
at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__188_0(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.<>c.<.cctor>b__272_0(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
Provider and version information
EF Core version: 6.0.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Windows 10 Pro 21H2
IDE: Visual Studio 2022 17.1.2
The text was updated successfully, but these errors were encountered:
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.
roji
changed the title
Collation conflict with SaveChangesAsync using Merge
Collation conflict when using the legacy SaveChanges behavior (temporary table variable)
Dec 9, 2022
There is a collation issue with SaveChangesAsync when the Merge is used (ValueGeneratedOnAdd).
Code sample
Error
Provider and version information
EF Core version: 6.0.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Windows 10 Pro 21H2
IDE: Visual Studio 2022 17.1.2
The text was updated successfully, but these errors were encountered: