dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.73k stars 3.18k forks source link

Method not found: 'Void Microsoft.EntityFrameworkCore.Update.IUpdateEntry.SetStoreGeneratedValue(Microsoft.EntityFrameworkCore.Metadata.IProperty, System.Object)'. in call method _dbContext.SaveChangesAsync() in efCore 7.0.0-rc.2.22472.11 #29410

Closed mooriyami closed 1 year ago

mooriyami commented 1 year ago

Bug

After we upgrade our project to efcore version 7.0.0-rc.2.22472.11, when we call await _dbContext.SaveChangesAsync() to save changes in database, the changes affected database but we have this exception.

Exception

Method not found: 'Void Microsoft.EntityFrameworkCore.Update.IUpdateEntry.SetStoreGeneratedValue(Microsoft.EntityFrameworkCore.Metadata.IProperty, System.Object)'

Stack traces

at Microsoft.EntityFrameworkCore.Update.ColumnModification.set_Value(Object value) in /_/src/EFCore.Relational/Update/ColumnModification.cs:line 173
   at Microsoft.EntityFrameworkCore.Update.ModificationCommand.PropagateResults(RelationalDataReader relationalReader) in /_/src/EFCore.Relational/Update/ModificationCommand.cs:line 832
   at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.<ConsumeResultSetWithPropagationAsync>d__4.MoveNext() in /_/src/EFCore.Relational/Update/AffectedCountModificationCommandBatch.cs:line 359
   at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.<ConsumeAsync>d__2.MoveNext() in /_/src/EFCore.Relational/Update/AffectedCountModificationCommandBatch.cs:line 179

at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.<ConsumeAsync>d__2.MoveNext() in /_/src/EFCore.Relational/Update/AffectedCountModificationCommandBatch.cs:line 259
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.<ExecuteAsync>d__50.MoveNext() in /_/src/EFCore.Relational/Update/ReaderModificationCommandBatch.cs:line 400
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.<ExecuteAsync>d__50.MoveNext() in /_/src/EFCore.Relational/Update/ReaderModificationCommandBatch.cs:line 400
   at Microsoft.EntityFrameworkCore.SqlServer.Update.Internal.SqlServerModificationCommandBatch.<ExecuteAsync>d__14.MoveNext() in /_/src/EFCore.SqlServer/Update/Internal/SqlServerModificationCommandBatch.cs:line 233
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.<ExecuteAsync>d__9.MoveNext() in /_/src/EFCore.Relational/Update/Internal/BatchExecutor.cs:line 207
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.<ExecuteAsync>d__9.MoveNext() in /_/src/EFCore.Relational/Update/Internal/BatchExecutor.cs:line 230
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.<ExecuteAsync>d__9.MoveNext() in /_/src/EFCore.Relational/Update/Internal/BatchExecutor.cs:line 255
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<SaveChangesAsync>d__106.MoveNext() in /_/src/EFCore/ChangeTracking/Internal/StateManager.cs:line 1226
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<SaveChangesAsync>d__110.MoveNext() in /_/src/EFCore/ChangeTracking/Internal/StateManager.cs:line 1325
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.<ExecuteAsync>d__7`2.MoveNext() in /_/src/EFCore.SqlServer/Storage/Internal/SqlServerExecutionStrategy.cs:line 79
   at Microsoft.EntityFrameworkCore.DbContext.<SaveChangesAsync>d__63.MoveNext() in /_/src/EFCore/DbContext.cs:line 771
   at Microsoft.EntityFrameworkCore.DbContext.<SaveChangesAsync>d__63.MoveNext() in /_/src/EFCore/DbContext.cs:line 809
   at KCC.Common.DAL.BaseRepository.<CommitAsync>d__43.MoveNext() in C:\Kia\Saman\azure\SamanV2\KKR.SamanV2\Common\KCC.CommonV2\BaseClasses\BaseRepository.cs:line 520

Output

Microsoft.EntityFrameworkCore.Database.Command: Information: Executed DbCommand (688ms) [Parameters=[@p0='3000e9a1-3d17-428d-b4f7-c7fb8bb99ebf' (Nullable = true), @p1=NULL (Size = 4000), @p2='b54b2c15-b462-4bab-89cf-12c0bee9c95a' (Nullable = true), @p3='2022-10-23T09:53:38.3753356+03:30' (Nullable = true), @p4='root - root' (Size = 4000), @p5='9e05699b-c548-4d06-b1f6-c059307a6cca' (Nullable = true), @p6='root' (Size = 4000), @p7=NULL (Size = 50), @p8='bdb6ceb5-8397-4a00-b897-2ff19b8e1296', @p9='d9eea853-b079-4f96-83c5-9f41fd624b59' (Nullable = true), @p10='False', @p11=NULL (DbType = DateTime2), @p12=NULL (Size = 4000), @p13=NULL (DbType = Guid), @p14=NULL (Size = 4000), @p15=NULL (Size = 4000), @p16='c2196b6e-03bc-4e06-9fc1-b86d5f365e57', @p17='2f12b825-8d5a-4f67-a789-40a305547550'], CommandType='Text', CommandTimeout='30']
SET IMPLICIT_TRANSACTIONS OFF;
SET NOCOUNT ON;
INSERT INTO [DelegationOfAuthority] ([BranchGuid], [Comment], [CompanyGuid], [CreateDate], [CreatePersonFullName], [CreateUserGuid], [CreateUserName], [ExpairDate], [FromUserId], [Guid], [IsDisabled], [LastModifiedDate], [LastModifiedPersonFullName], [LastModifiedUserGuid], [LastModifiedUserName], [RequestTypeRestrictions], [SamanRolId], [ToUserId])
OUTPUT INSERTED.[Id]
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12, @p13, @p14, @p15, @p16, @p17);
Exception thrown: 'System.MissingMethodException' in Microsoft.EntityFrameworkCore.Relational.dll
Exception thrown: 'System.MissingMethodException' in System.Private.CoreLib.dll
Exception thrown: 'Microsoft.EntityFrameworkCore.DbUpdateException' in Microsoft.EntityFrameworkCore.Relational.dll
Exception thrown: 'Microsoft.EntityFrameworkCore.DbUpdateException' in System.Private.CoreLib.dll
Exception thrown: 'Microsoft.EntityFrameworkCore.DbUpdateException' in System.Private.CoreLib.dll
Exception thrown: 'Microsoft.EntityFrameworkCore.DbUpdateException' in System.Private.CoreLib.dll
Exception thrown: 'Microsoft.EntityFrameworkCore.DbUpdateException' in System.Private.CoreLib.dll
Exception thrown: 'Microsoft.EntityFrameworkCore.DbUpdateException' in System.Private.CoreLib.dll
Exception thrown: 'Microsoft.EntityFrameworkCore.DbUpdateException' in System.Private.CoreLib.dll
Exception thrown: 'Microsoft.EntityFrameworkCore.DbUpdateException' in System.Private.CoreLib.dll
Exception thrown: 'Microsoft.EntityFrameworkCore.DbUpdateException' in System.Private.CoreLib.dll
Exception thrown: 'Microsoft.EntityFrameworkCore.DbUpdateException' in Microsoft.EntityFrameworkCore.dll
Exception thrown: 'Microsoft.EntityFrameworkCore.DbUpdateException' in System.Private.CoreLib.dll
Exception thrown: 'Microsoft.EntityFrameworkCore.DbUpdateException' in System.Private.CoreLib.dll
'SamanshahrV2.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.0-rc.1.22426.10\System.Reflection.Metadata.dll'. Symbols loaded.
Microsoft.EntityFrameworkCore.Update: Error: An exception occurred in the database while saving changes for context type 'KKR.Saman.DataModels.MainDataModel'.
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
 ---> System.MissingMethodException: Method not found: 'Void Microsoft.EntityFrameworkCore.Update.IUpdateEntry.SetStoreGeneratedValue(Microsoft.EntityFrameworkCore.Metadata.IProperty, System.Object)'.
   at Microsoft.EntityFrameworkCore.Update.ColumnModification.set_Value(Object value)
   at Microsoft.EntityFrameworkCore.Update.ModificationCommand.PropagateResults(RelationalDataReader relationalReader)
   at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ConsumeResultSetWithPropagationAsync(Int32 startCommandIndex, RelationalDataReader reader, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ConsumeAsync(RelationalDataReader reader, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.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.SqlServer.Update.Internal.SqlServerModificationCommandBatch.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 Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)

Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
 ---> System.MissingMethodException: Method not found: 'Void Microsoft.EntityFrameworkCore.Update.IUpdateEntry.SetStoreGeneratedValue(Microsoft.EntityFrameworkCore.Metadata.IProperty, System.Object)'.
   at Microsoft.EntityFrameworkCore.Update.ColumnModification.set_Value(Object value)
   at Microsoft.EntityFrameworkCore.Update.ModificationCommand.PropagateResults(RelationalDataReader relationalReader)
   at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ConsumeResultSetWithPropagationAsync(Int32 startCommandIndex, RelationalDataReader reader, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ConsumeAsync(RelationalDataReader reader, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.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.SqlServer.Update.Internal.SqlServerModificationCommandBatch.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 Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
Exception thrown: 'Microsoft.EntityFrameworkCore.DbUpdateException' in System.Private.CoreLib.dll
Exception thrown: 'Microsoft.EntityFrameworkCore.DbUpdateException' in System.Private.CoreLib.dll

Provider and version information

EF Core version: 7.0.0-rc.2.22472.11 Database provider: Microsoft.EntityFrameworkCore.SqlServer

roji commented 1 year ago

@mooriyami you are very likely mixing different versions of EF Core; check your csproj to make sure all EF-related packages are the same (e.g. 7.0.0-rc.2). If you can't find an issue, please post your csproj here.

mooriyami commented 1 year ago

Thanks @roji , I checked it, The packages are same. my csproj is :

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <UserSecretsId>99b34739-414a-487b-8cd9-dc3ffc7fe01d</UserSecretsId>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
    <PackageReference Include="Inflector.NetStandard" Version="1.2.2" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.0-rc.2.22476.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0-rc.2.22472.11" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="7.0.0-rc.2.22472.11" />
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
    <PackageReference Include="Quartz" Version="3.5.0" />
    <PackageReference Include="Quartz.NetCore.DependencyInjection" Version="5.0.0" />
    <PackageReference Include="RestSharp" Version="108.0.3-alpha.0.5" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
    <PackageReference Include="System.Data.OleDb" Version="7.0.0-rc.2.22472.3" />
    <PackageReference Include="System.ServiceModel.Duplex" Version="4.10.0" />
    <PackageReference Include="System.ServiceModel.Federation" Version="4.10.0" />
    <PackageReference Include="System.ServiceModel.Http" Version="4.10.0" />
    <PackageReference Include="System.ServiceModel.NetTcp" Version="4.10.0" />
    <PackageReference Include="System.ServiceModel.Security" Version="4.10.0" />
  </ItemGroup>  

</Project>
ajcvickers commented 1 year ago

@mooriyami There is no EF Core provider referenced there, which means some other package is pulling in an old version of the Microsoft.EntityFrameworkCore.SqlServer by a transitive reference.

mooriyami commented 1 year ago

Thanks @ajcvickers, I had another project with Ef core provider that was referenced in my project but I removed extra parts in the code so I did not no have no PackageReference for that in previous csproj . As you said, I referenced EF Core provider but it does not work and the new csproj is like this:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <UserSecretsId>99b34739-414a-487b-8cd9-dc3ffc7fe01d</UserSecretsId>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
    <PackageReference Include="Inflector.NetStandard" Version="1.2.2" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.0-rc.2.22476.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0-rc.2.22472.11" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="7.0.0-rc.2.22472.11" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.0-rc.1.22426.7" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0-rc.1.22426.7" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite" Version="7.0.0-rc.1.22426.7" />
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
    <PackageReference Include="Quartz" Version="3.5.0" />
    <PackageReference Include="Quartz.NetCore.DependencyInjection" Version="5.0.0" />
    <PackageReference Include="RestSharp" Version="108.0.3-alpha.0.5" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
    <PackageReference Include="System.Data.OleDb" Version="7.0.0-rc.2.22472.3" />
    <PackageReference Include="System.ServiceModel.Duplex" Version="4.10.0" />
    <PackageReference Include="System.ServiceModel.Federation" Version="4.10.0" />
    <PackageReference Include="System.ServiceModel.Http" Version="4.10.0" />
    <PackageReference Include="System.ServiceModel.NetTcp" Version="4.10.0" />
    <PackageReference Include="System.ServiceModel.Security" Version="4.10.0" />
  </ItemGroup>

</Project>
ErikEJ commented 1 year ago

Now you are mixing rc1 and rc2 packages...