2011-12-17 122 views
3

我试图更新实体,但我收到以下错误:实体框架更新 - 上下文当前没有跟踪实体

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.

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

回答

2

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

基本上,这意味着你可以通过“固定”实例直接更新实体。尝试使用对UpdateObject方法的调用删除该行。

+0

嗨Carko,你说得对。不得不更新实际夹具!非常感谢 – Johann 2011-12-17 23:27:41