【发布时间】:2021-08-13 10:57:22
【问题描述】:
我在开发小型 Blazor WASM 应用程序时遇到了问题。 我的应用程序的一部分是用户可以创建团队并邀请其他用户加入他们的团队的地方。相关的实体类是:
Team.cs
public class Team
{
[Key]
public Guid TeamID { get; set; }
public string Name { get; set; }
public string Abbreviation { get; set; }
public Guid? BadgeID { get; set; }
public Guid TownID { get; set; }
public Guid StatisticsID { get; set; }
public Guid CaptainID { get; set; }
public List<AppUserDTO> Players { get; set; } = new();
}
当用户接受邀请时,他应该被添加到List<AppUserDTO> Players 列表中,我在客户端这样做:
private async Task AcceptInvite()
{
Team.Players.Add(Player);
await TeamDataService.UpdateTeam(Team);
}
public async Task UpdateTeam(Team team)
{
var teamJson =
new StringContent(JsonSerializer.Serialize(team), Encoding.UTF8, "application/json");
await _httpClient.PutAsync("api/team", teamJson);
}
但是当我想将更改保存到服务器时,我在服务器端收到以下异常:
System.InvalidOperationException: The instance of entity type 'AppUserDTO' 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.
服务器端代码为:
public Team UpdateTeam(Team team)
{
var updatedTeam = _appDbContext.Teams.Include(t => t.Players).FirstOrDefault(t => t.TeamID == team.TeamID);
if (updatedTeam == null) return null;
updatedTeam.TeamID = team.TeamID;
updatedTeam.Name = team.Name;
updatedTeam.Abbreviation = team.Abbreviation;
updatedTeam.TownID = team.TownID;
updatedTeam.StatisticsID = team.StatisticsID;
updatedTeam.Players = team.Players;
updatedTeam.CaptainID = team.CaptainID;
_appDbContext.SaveChanges();
return updatedTeam;
}
在 _appDbContext.SaveChanges() 方法处弹出异常。
我注意到以下内容:当我将一个实体添加到一个空列表并保存它时,我没有收到任何异常,但如果列表已经有实体,我会收到此错误。
对此有什么解决方案,我相信这是我尝试做的很常见的事情,但我在任何地方都没有找到解决方案。
【问题讨论】:
-
也许
updatedTeam.Players.AddRange(team.Players)? -
@SvyatoslavDanyliv 同样的例外。在我看来,您的建议没有意义,因为 updatedTeam 已经有一些球员,而 team 将有相同的球员 + 1,即将加入。
-
这里很奇怪,为什么
Team有AppUserDTO类。 DTO 不应成为 DB 模型的一部分。
标签: entity-framework entity-framework-core blazor blazor-webassembly