2011-06-03 36 views
0

這是繼續我的problem here。我正在嘗試使用solution Julie Lerman gave me a few months ago。我目前使用以下生成一個新的遊戲實體預先貼在我的ObjectContext:EF4.0 - 有沒有辦法在調試過程中看到哪些實體附加到ObjectContext上?

Game game = _gameRepository.GetGame(formData.GameID); 
AutoMapper.Mapper.Map<AdminGameEditModel, Game>(formData, game); 

在庫中,我嘗試將遊戲安裝到其狀態設置爲「添加」喜歡她的OC通過執行以下操作建議:

public Game GetGame(int id) 
{ 
    if (id > 0) 
    { 
     return _siteDB.Games.Include("Genre").Include("Platforms").SingleOrDefault(g => g.GameID == id); 
    } 
    else 
    { 
     Game game = _siteDB.Games.CreateObject(); 
     _siteDB.Games.AddObject(game); 
     return game; 
    } 
} 

現在,爲了清楚起見,這裏是我的控制器在其整體的構造:

public AdminController(IArticleRepository articleRepository, IGameRepository gameRepository, INewsRepository newsRepository) 
{ 
    _articleRepository = articleRepository; 
    _gameRepository = gameRepository; 
    _newsRepository = newsRepository; 

    Mapper.CreateMap<AdminGameEditModel, Game>() 
     .BeforeMap((s, d) => 
     { 
      if (d.Platforms.Count > 0) 
      { 
       Platform[] existing = d.Platforms.ToArray(); 

       foreach (var plat in existing) 
       { 
        d.Platforms.Remove(plat); 
       } 
      } 

      foreach (var platId in s.PlatformIDs) 
      { 
       Platform newPlat = _gameRepository.GetPlatform(platId); 
       d.Platforms.Add(newPlat); 
      } 
     }) 
     .ForMember(dest => dest.BoxArtPath, opt => opt.Ignore()) 
     .ForMember(dest => dest.IndexImagePath, opt => opt.Ignore()) 
     .ForMember(dest => dest.Cons, opt => opt.MapFrom(src => String.Join("|", src.Cons))) 
     .ForMember(dest => dest.Pros, opt => opt.MapFrom(src => String.Join("|", src.Pros))) 
     .ForMember(dest => dest.LastModified, opt => opt.UseValue(DateTime.Now)) 
     .ForMember(dest => dest.Platforms, opt => opt.Ignore()); 
} 

正如你所看到的,_gameRepository應爲t他自從在控制器構建中創建以來就一直如此。這反過來又意味着_gameRepository的OC應該與遊戲和平臺相同。然而,在這種情況下,我仍然得到一個例外,其中指出:

無法定義兩個對象之間的關係,因爲它們連接到不同的ObjectContext對象。

某些事情確實會發生,這就是爲什麼我想知道我是否可以實際跟蹤實體實際連接到的ObjectContext。他們都應該隸屬於同一個法定組織,但例外情況另有說明。

也許它與我使用Ninject(香草版本,而不是 MVC定製版本)在控制器中注入存儲庫有關。無論問題是什麼,這似乎都不明顯。任何幫助將大大讚賞。

編輯:庫的ObjectContext的:

public class HGGameRepository : IGameRepository 
{ 
    private HGEntities _siteDB = new HGEntities(); 

    // rest of class code 
} 

Ninject綁定:

private class HandiGamerServices : NinjectModule 
{ 
    public override void Load() 
    { 
     Bind<IArticleRepository>().To<HGArticleRepository>().InRequestScope(); 
     Bind<IGameRepository>().To<HGGameRepository>().InRequestScope(); 
     Bind<INewsRepository>().To<HGNewsRepository>().InRequestScope(); 
     Bind<ErrorController>().ToSelf().InRequestScope(); 
    } 
} 

回答

2

你可以問ObjectContext的,如果它有通過一定對象的引用:

ObjectStateEntry ose; 
bool isInContext = someContext.ObjectStateManager.TryGetObjectStateEntry(someObject, out ose); 
+0

太棒了!謝謝 :) – 2011-06-03 21:24:26

1

有問題的部分是

public class HGGameRepository : IGameRepository 
{ 
    private HGEntities _siteDB = new HGEntities(); 

    // rest of class code 
} 

我相信所有的庫創建自己的新環境時,創建。取而代之的是,你應該使用構造器注入

public class HGGameRepository : IGameRepository 
{ 
    private HGEntities _siteDB; 

    public HGGameRepository(HGEntities entities) 
    { 
      _siteDB= entities 
    } 
} 

然後你Ninject模塊中包括這個

Bind<HGEntities>().ToSelf().InRequestScope(); 

這樣,你的倉庫將共享相同的背景。

+0

沒有這樣的運氣,得到了同樣的異常。 – 2011-06-03 19:17:53

+0

爲了得到它的工作,你還必須在你的控制器構造函數中傳入HGEntities,如下所示: public AdminController(HGEntities entities,IArticleRepository articleRepository等) – 2012-03-29 22:52:35

相關問題