Replies: 1 comment 2 replies
-
步骤一,在查询数据时加上 AsNoTracking |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
BootstrapBlazor + Server 用 EF Core + MS Sql Server 作为数据源后,本人小白在实战过程中发现有些问题,不知哪位大佬可以解疑?

1、
按以上的 Table 设置后,添加、删除、过滤等功能都可以正常使用,数据库里的实际数据也发现变化,但是编辑功能无法正常使用,点击“取消/保存”按钮出现如下异常。


warn: Microsoft.AspNetCore.Components.Server.Circuits.RemoteRenderer[100]
Unhandled exception rendering component: The instance of entity type 'Customer' cannot be tracked because another instance with the key value '{CustomerId: 19}' is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.
System.InvalidOperationException: The instance of entity type 'Customer' cannot be tracked because another instance with the key value '{CustomerId: 19}' is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap
1.ThrowIdentityConflict(InternalEntityEntry entry) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap
1.Add(TKey key, InternalEntityEntry entry, Boolean updateDuplicate)at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap
1.Add(TKey key, InternalEntityEntry entry) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap
1.Add(InternalEntityEntry entry)at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTracking(InternalEntityEntry entry)
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.EntityGraphAttacher.PaintAction(EntityEntryGraphNode
1 node) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[TState](EntityEntryGraphNode
1 node, Func2 handleNode) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState targetState, EntityState storeGeneratedWithKeySetTargetState, Boolean forceStateWhenUnknownKey) at Microsoft.EntityFrameworkCore.DbContext.SetEntityState(InternalEntityEntry entry, EntityState entityState) at Microsoft.EntityFrameworkCore.DbContext.SetEntityState[TEntity](TEntity entity, EntityState entityState) at Microsoft.EntityFrameworkCore.DbContext.Update[TEntity](TEntity entity) at BootstrapBlazor.DataAccess.EntityFrameworkCore.DefaultDataService
1.SaveAsync(TModel model, ItemChangedType changedType)at BootstrapBlazor.Components.Table
1.InternalOnSaveAsync(TItem item, ItemChangedType changedType) at BootstrapBlazor.Components.Table
1.SaveModelAsync(EditContext context, ItemChangedType changedType)at BootstrapBlazor.Components.Table
1.OnSaveEditCallbackAsync(EditContext context, ItemChangedType changedType) at BootstrapBlazor.Components.Table
1.<>c__DisplayClass1517_0.<b__1>d.MoveNext()--- End of stack trace from previous location ---
at BootstrapBlazor.Components.TableEditDialogOptionExtensions.<>c__DisplayClass0_0
1.<<ToParameter>b__1>d.MoveNext() --- End of stack trace from previous location --- at BootstrapBlazor.Components.EditDialog
1.OnValidSubmitAsync(EditContext context)at BootstrapBlazor.Components.ValidateForm.OnValidSubmitForm(EditContext context)
at BootstrapBlazor.Components.BootstrapComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at BootstrapBlazor.Components.BootstrapComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]
Unhandled exception in circuit '67Snb4hvPuhDymBZ0W4pMC1sMShVy4Z4r_faoZS8zro'.
System.InvalidOperationException: The instance of entity type 'Customer' cannot be tracked because another instance with the key value '{CustomerId: 19}' is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap
1.ThrowIdentityConflict(InternalEntityEntry entry) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap
1.Add(TKey key, InternalEntityEntry entry, Boolean updateDuplicate)at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap
1.Add(TKey key, InternalEntityEntry entry) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap
1.Add(InternalEntityEntry entry)at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTracking(InternalEntityEntry entry)
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.EntityGraphAttacher.PaintAction(EntityEntryGraphNode
1 node) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[TState](EntityEntryGraphNode
1 node, Func2 handleNode) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState targetState, EntityState storeGeneratedWithKeySetTargetState, Boolean forceStateWhenUnknownKey) at Microsoft.EntityFrameworkCore.DbContext.SetEntityState(InternalEntityEntry entry, EntityState entityState) at Microsoft.EntityFrameworkCore.DbContext.SetEntityState[TEntity](TEntity entity, EntityState entityState) at Microsoft.EntityFrameworkCore.DbContext.Update[TEntity](TEntity entity) at BootstrapBlazor.DataAccess.EntityFrameworkCore.DefaultDataService
1.SaveAsync(TModel model, ItemChangedType changedType)at BootstrapBlazor.Components.Table
1.InternalOnSaveAsync(TItem item, ItemChangedType changedType) at BootstrapBlazor.Components.Table
1.SaveModelAsync(EditContext context, ItemChangedType changedType)at BootstrapBlazor.Components.Table
1.OnSaveEditCallbackAsync(EditContext context, ItemChangedType changedType) at BootstrapBlazor.Components.Table
1.<>c__DisplayClass1517_0.<b__1>d.MoveNext()--- End of stack trace from previous location ---
at BootstrapBlazor.Components.TableEditDialogOptionExtensions.<>c__DisplayClass0_0
1.<<ToParameter>b__1>d.MoveNext() --- End of stack trace from previous location --- at BootstrapBlazor.Components.EditDialog
1.OnValidSubmitAsync(EditContext context)at BootstrapBlazor.Components.ValidateForm.OnValidSubmitForm(EditContext context)
at BootstrapBlazor.Components.BootstrapComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at BootstrapBlazor.Components.BootstrapComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
2、
按以上的 Table 设置后,添加、删除、修改等功能都可以使用,但是只是页面的数据发现变化,数据库里的实现数据没有变化。
Program.cs 里面的数据库设置如下:
不知有没有遇到同样问题的大佬,请不胜指点!
Beta Was this translation helpful? Give feedback.
All reactions