【问题标题】:EF4.0 - Is there a way to see what entities are attached to what ObjectContext during debugging?EF4.0 - 有没有办法在调试期间查看哪些实体附加到什么 ObjectContext?
【发布时间】:2011-09-07 11:55:26
【问题描述】:

这是我problem here的延续。

我正在尝试使用solution Julie Lerman gave me a few months ago。我目前正在使用以下内容生成预先附加到我的 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 应该是相同的,因为它是在控制器构造时创建的。反过来,这意味着 _gameRepository 的 OC 对于游戏和平台应该是相同的。然而,在这种情况下,我仍然收到一个例外情况:

无法定义两个对象之间的关系,因为它们附加到不同的 ObjectContext 对象。

肯定会发生一些奇怪的事情,这就是为什么我想知道我是否可以真正追踪实体实际附加到哪个 ObjectContext。它们都应该附加到同一个 OC,但例外情况除外。

也许它与我使用 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();
    }
}

【问题讨论】:

    标签: c# asp.net-mvc-2 entity-framework-4 ninject objectcontext


    【解决方案1】:

    您可以通过以下方式询问 ObjectContext 是否引用了某个对象:

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

    【讨论】:

      【解决方案2】:

      有问题的部分是这个

      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();
      

      这样您的存储库将共享相同的上下文。

      【讨论】:

      • 没有这样的运气,得到同样的例外。
      • 要让它工作,你还必须在你的控制器构造函数中传递 HGEntities,像这样:public AdminController(HGEntities entity, IArticleRepository articleRepository, etc)
      猜你喜欢
      • 1970-01-01
      • 2015-10-19
      • 1970-01-01
      • 1970-01-01
      • 2011-06-04
      • 2012-07-18
      • 1970-01-01
      • 2020-05-04
      • 2021-11-16
      相关资源
      最近更新 更多