Skip to content

EFCore 7.0 Bug - Invalid Operation Exception RE "Temporary Value" #29570

@TonyValenti

Description

@TonyValenti

File a bug

Hi All,
I just upgraded my app to EF7.0 and I'm bumping into what I believe is new bug.

Related to my investigation, I've noticed a few things that either don't seem quite right or aren't quite what I would expect.

Here is my scenario. I am inserting:

  • 1000 rows
  • Using 1 "SaveChanges"
  • Into 1 table
  • Which has 57 columns (most of which are nvarchar(MAX))
  • And uses Owned Entities
  • And has a unique constraint on one of the string columns
  • The primary key is a DB-generated long named Row which is never set in my code.

Here are some odd things I've noticed:

  • AddRange takes 4-6 seconds!
  • Per SQL Profiler, EFCore seems to split the command into multiple sp_executesql commands.
  • Some commands start with SET IMPLICIT_TRANSACTIONS OFF;SET NOCOUNT ON; and some start with just SET NOCOUNT ON;
  • Some commands INSERT INTO and others MERGE
  • EFCore errors with:
The property 'ExpenseActivityImportJob.Row' has a temporary value while attempting to change the entity's state to 'Unchanged'. Either set a permanent value explicitly, or ensure that the database is configured to generate values for this property.
  • The database is configured to generate values for that column and I never set it via code.
  • Even though I get the above error, 900ish rows actually get inserted into the database (I thought it should be all or nothing?)

What should I do next in terms of troubleshooting this?

Something else I thought of:

  • When inserting multiple rows at once, it is conceivable that there will be many shared values between the different rows (at least there are in my data). I think the generated SQL could be more efficient if it deduplicated parameter values before using them. For example, maybe @p1522 gets used everywhere a certain value comes into play. I suspect this would reduce network latency and the size of the SQL commands sent to SQL server.

Include your code

I can't at this time.

Include stack traces

   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges, Boolean modifyProperties)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Boolean modifyProperties, Nullable`1 forceStateWhenUnknownKey, Nullable`1 fallbackState)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.AcceptChanges()
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.AcceptAllChanges(IReadOnlyList`1 changedEntries)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(StateManager stateManager, Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at UniversalMigrator.Storage.MigrationDbContext.SaveChanges(Boolean acceptAllChangesOnSuccess) in C:\Users\TonyV\source\repos\MediatedCommunications\UniversalMigrator\UniversalMigrator.Storage\Context\MigrationDbContext.cs:line 127
   at Framework.Storage.DbContextExtensions.<SaveChangesFastAsync>d__0.MoveNext() in C:\Users\TonyV\source\repos\MediatedCommunications\Framework\Framework.Storage\Microsoft\EntityFrameworkCore\DbContextExtensions.cs:line 14
   at UniversalMigrator.Agents.DataflowExtractorAgent.<>c__DisplayClass26_0.<<SaveActionAsync>b__2>d.MoveNext() in C:\Users\TonyV\source\repos\MediatedCommunications\UniversalMigrator\UniversalMigrator\Agents.Bases\DataflowExtractorAgent.cs:line 137

Include provider and version information

EF Core version: 7.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 7.0
Operating system: Win11
IDE: Visual Studio 2022 17.4

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions