2011-12-17 57 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