efcore: Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException
ok so my EF Core suddently stopped working when updating Microsoft.EntityFramework.Tools, Design to 7.0.0-rc.1.22426.7 and i am using the Postgres SQL provider which is updated to 7.0.0-rc.1… i am using the same modals as i had on the lasted updated and i thought the DB was beging weird so i created a new Table and the problem is still, it seems to be working with some modal classes but not my main ones My Modal Class https://pastebin.com/iHWN54Fh i have a method directly into my Context that checks if the member is added or not
public async Task<KurumiMember> GetOrAddMemberAsync(Snowflake guildId, Snowflake memberId)
{
KurumiMember? member = await GetMemberAsync(guildId, memberId);
if (member is not null)
return member;
member = new(guildId, memberId);
return member;
}
Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: The database operation was expected to affect 1 row(s), but actually affected 0 row(s); data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.
at Npgsql.EntityFrameworkCore.PostgreSQL.Update.Internal.NpgsqlModificationCommandBatch.ThrowAggregateUpdateConcurrencyExceptionAsync(RelationalDataReader reader, Int32 commandIndex, Int32 expectedRowsAffected, Int32 rowsAffected, CancellationToken cancellationToken)
at Npgsql.EntityFrameworkCore.PostgreSQL.Update.Internal.NpgsqlModificationCommandBatch.ConsumeAsync(RelationalDataReader reader, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IList`1 entriesToSave, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(StateManager stateManager, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
at KurumiBot.Services.InteractionsService.SetPartnerAsync(Snowflake guildId, Snowflake memberId, Snowflake partnerId) in C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot\Services\InteractionsService.cs:line 243
at KurumiBot.Services.InteractionsService.SetPartnerAsync(Snowflake guildId, Snowflake memberId, Snowflake partnerId) in C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot\Services\InteractionsService.cs:line 244
at KurumiBot.Services.InteractionsService.SetPartnerAsync(Snowflake guildId, Snowflake memberId, Snowflake partnerId) in C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot\Services\InteractionsService.cs:line 245
at KurumiBot.Commands.Modules.TextModules.UserInteractionModule.MarryAsync(IMember partner) in C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot\Commands\Modules\TextModules\UserInteractionModule.cs:line 145
at Qmmands.ReflectionCommandCallback.ExecuteAsync(ICommandContext context)
at Qmmands.ReflectionCommandCallback.ExecuteAsync(ICommandContext context)
at Qmmands.Default.DefaultExecutionSteps.ExecuteCommand.OnExecuted(ICommandContext context)
PS C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot> dotnet ef dbcontext list --verbose
Using project 'C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot\KurumiBot.csproj'.
Using startup project 'C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot\KurumiBot.csproj'.
Writing 'C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot\obj\KurumiBot.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\Tim\AppData\Local\Temp\tmp12F1.tmp /verbosity:quiet /nologo C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot\KurumiBot.csproj
Writing 'C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot\obj\KurumiBot.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\Tim\AppData\Local\Temp\tmp1553.tmp /verbosity:quiet /nologo C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot\KurumiBot.csproj
Build started...
dotnet build C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot\KurumiBot.csproj /verbosity:quiet /nologo
Der Buildvorgang wurde erfolgreich ausgeführt.
0 Warning(s)
0 Errors
Verstrichene Zeit 00:00:03.78
Build succeeded.
dotnet exec --depsfile C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot\bin\Debug\net6.0\KurumiBot.deps.json --additionalprobingpath C:\Users\Tim\.nuget\packages --runtimeconfig C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot\bin\Debug\net6.0\KurumiBot.runtimeconfig.json C:\Users\Tim\.dotnet\tools\.store\dotnet-ef\7.0.0-rc.1.22426.7\dotnet-ef\7.0.0-rc.1.22426.7\tools\net6.0\any\tools\netcoreapp2.0\any\ef.dll dbcontext list --assembly C:\Users\Tim\RiderProjects\kurumi-bot\
src\KurumiBot\bin\Debug\net6.0\KurumiBot.dll --project C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot\KurumiBot.csproj --startup-assembly C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot\bin\Debug\net6.0\KurumiBot.dll --startup-project C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot\KurumiBot.csproj --project-dir C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot\ --root-namespace KurumiBot --language C# --framework net6.0 --nullable --working-dir C:\Users\Tim\Rider
Projects\kurumi-bot\src\KurumiBot --verbose
Using assembly 'KurumiBot'.
Using startup assembly 'KurumiBot'.
Using application base 'C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot\bin\Debug\net6.0'.
Using working directory 'C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot'.
Using root namespace 'KurumiBot'.
Using project directory 'C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'KurumiBot'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Microsoft.Extensions.Hosting.HostAbortedException: The host was aborted.
at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.ThrowHostAborted()
at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.OnNext(KeyValuePair`2 value)
at System.Diagnostics.DiagnosticListener.Write(String name, Object value)
at Microsoft.Extensions.Hosting.HostBuilder.Write[T](DiagnosticSource diagnosticSource, String name, T value)
at Microsoft.Extensions.Hosting.HostBuilder.ResolveHost(IServiceProvider serviceProvider, DiagnosticListener diagnosticListener)
at Microsoft.Extensions.Hosting.HostBuilder.Build()
at Program.<Main>$(String[] args) in C:\Users\Tim\RiderProjects\kurumi-bot\src\KurumiBot\Program.cs:line 62
Using application service provider from Microsoft.Extensions.Hosting.
Found DbContext 'KurumiContext'.
Finding DbContext classes in the project...
EF Core version: Database provider: Npgsql.EntityFrameworkCore.PostgreSQL Target framework:.NET6.0 Operating system: IDE: JetBrains Rider newest version
My Service code https://pastebin.com/p8FZgLPq
Last one i am really new to this, and sorry for my bad english.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 16 (8 by maintainers)
@LedgendODST you didn’t mention anything above about version 5.0; but regardless, your non-working code fails for me using Npgsql.EntityFrameworkCore.PostgreSQL 5.0.10 as well - as expected. I’ve taken your code and added EnsureDeleted/EnsureCreated at the beginning - since I have no idea what the starting state of your database is - and tried running the code both when the row already exists and when it doesn’t (see comment below).
Once again, you probably want to read up on EF’s change tracking, and exactly what Update does. Your code shouldn’t be working in any version of EF. However, if you can submit code that actually works on an older version and fails on a newer one, I can take another look.
Attempted repro
@LedgendODST at this point I’m pretty confused about exactly what worked and stopped working in 7.0. You’ll have to submit a clear code sample, since the one you submitted uses EF incorrectly and also fails on 6.0.
@LedgendODST the Snowflake type is missing. In addition, in Program.cs you load the DatabaseModel with a given ID, but then you create a new instance with the same ID. You then change a property on that new instance (
user.Ids.Add(1)
), but EF isn’t aware of that new instance. As a result SaveChangesAsync doesn’t seem to do anything at all… Below, I’ve tried to recreate a simple version of your code sample - please tweak that (or submit a similar, full code sample) to show the problem.Note: it seems like Snowflake is being used as an ID wrapper for ulong (as a type converter); PostgreSQL doesn’t have a ulong type (nor do other databases in general), so EF value-converts that yet again to a numeric, which isn’t a good idea for an ID. I’d recommend simply using long as your ID.
Attempted repro
Ofc once i get home from work I will make a test project and test it and post it in here