EF4.0 - 有没有办法在调试期间看到什么实体附加到什么ObjectContext? [英] EF4.0 - Is there a way to see what entities are attached to what ObjectContext during debugging?

查看:329
本文介绍了EF4.0 - 有没有办法在调试期间看到什么实体附加到什么ObjectContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是继续我的这里的问题

我试图使用解决方案Julie Lerman几个月前给了我。我正在使用以下内容生成一个预先附加到我的ObjectContext的新的游戏实体:

I'm trying to use the solution Julie Lerman gave me a few months ago. I'm currently using the following to generate a new Game entity pre-attached to my ObjectContext:

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

在存储库中,我尝试将游戏附加到OC,其状态设置为已添加像她通过以下做法建议的:

In the repository, I try to attach the Game to the OC with its state set to 'Added' like she suggested by doing the following:

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;
    }
}

现在,为了清楚起见,这里是我的控制器的构造函数它的整体:

Now, for clarity's sake, here's my controller's constructor in its entirety:

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应该是一样的。然而,就这样,我仍然收到一个例外:

As you can see, the _gameRepository should be the same, since its created on controller construction. This, in turn, means that the _gameRepository's OC should be the same for both the Game and Platforms. Yet, with that being the case, I'm still getting an exception which states:


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

The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.

某些东西一定会发生,这就是为什么我想知道我是否可以追踪实体所附的ObjectContext。他们都应该被附加在同一个OC上,但例外则另有说明。

Something screwy is definitely going on, which is why I want to know if I can actually trace which ObjectContext the entities are actually attached to. They should all be attached to the same OC, but the exception claims otherwise.

也许这与我使用Ninject有关(香草版,不是 MVC定制版本)将控制器中的存储库注入。无论问题如何,几乎看不出来。任何帮助将非常赞赏。

Maybe it has something to do with me using Ninject (the vanilla version, not the MVC tailored version) to inject the repositories in the controller. Whatever the problem is, it hardly seems obvious. Any help would be greatly appreciated.

编辑:存储库的ObjectContext:

Repository's ObjectContext:

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

    // rest of class code
}

Ninject bindings :

Ninject bindings:

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


推荐答案

你可以通过以下方式向ObjectContext询问对象:

You can ask the ObjectContext if it has a reference to a certain object by:

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

这篇关于EF4.0 - 有没有办法在调试期间看到什么实体附加到什么ObjectContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆