gpt4 book ai didi

entity-framework-4 - Entity Framework 更新 - 上下文当前未跟踪实体

转载 作者:行者123 更新时间:2023-12-02 02:20:21 26 4
gpt4 key购买 nike

我正在尝试更新实体,但出现以下错误:

The context is not currently tracking the entity.

我的数据库表由以下字段组成:

fixturedate, leagueID (FK), Team A (FK), Team B (FK).

我的代码如下:

public void UpdateFixture(Fixture validFixture)
{
Fixture fixture = new Fixture();
fixture = entities.Fixtures.Where(f => f.fixtureId == validFixture.fixtureId).FirstOrDefault();

League league = new League();
league.LeagueId = validFixture.leagueId;
fixture.League = leagueActions.GetLeague(league);

fixture.Team1 = teamActions.GetTeam(validFixture.teamA);
fixture.Team2 = teamActions.GetTeam(validFixture.teamB);

entities.UpdateObject(validFixture);
entities.SaveChanges();
}

当我执行 entities.AttachTo("Fixtures", validFixture); 时,出现以下错误:

The context is already tracking a different entity with the same resource Uri.

我需要做什么来更新夹具实体?

最佳答案

为什么未跟踪 validFixture 从您的代码中不清楚,但是您选择了一个与 validFixture 实例具有相同 ID 的 Fixture 实体,这意味着它现在正在通过“fixture”实例跟踪该实体。

基本上这意味着您可以直接通过“fixture”实例更新实体。尝试只删除调用 UpdateObject 方法的行。

关于entity-framework-4 - Entity Framework 更新 - 上下文当前未跟踪实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8547783/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com