Skip to content

一个上下文同时跟踪两个相同的实体 #739

Open
@cc3w

Description

@cc3w

Description

在更新前将实体的状态设置为Detached再更新,不报错,但是数据库没有更新。另说明还有一种情况,当结合Masa blazor时,在前端页面同时更新同一条记录也是会报同样的错误。但是单纯使用swagger测试并不会。
参考代码如下:
public async Task GetGenerateCodeAsync(AllMapTable mapTable)
{
Guid? tid = null;
//自动插入mapTable表
var table = await _mapTableRepository.FindAsync(t => t.EnTableName.Equals(mapTable));
if (table == null)
{
var description = EnumDescription.GetDescriptionAttribute(mapTable);
if (description == null)
{
throw new UserFriendlyException("获取枚举上的Description失败");
}
var command = new CreateMapTableCommand(description, mapTable.ToString());
await _eventBus.PublishAsync(command);
tid = command.Id;
}
await _codingRuleInfoRepository.GenerateCode(mapTable);

    var tt = await _codingRuleInfoRepository.FindAsync(t => t.MapTableId == (Guid)tid);

    tt.CurrentValue = 5;

    await _codingRuleInfoRepository.UpdateAsync(tt);

    return "111";

}
报错如下:System.InvalidOperationException: The instance of entity type 'CodingRuleInfo' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.

at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap1.ThrowIdentityConflict(InternalEntityEntry entry) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap1.Add(TKey key, InternalEntityEntry entry, Boolean updateDuplicate)···

.NET version

No response

MASA Framework version

No response

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions