EF4.0 - 有没有办法,看看实体连接到调试过程中ObjectContext的是什么? [英] EF4.0 - Is there a way to see what entities are attached to what ObjectContext during debugging?

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

问题描述

这是我的<延续href=\"http://stackoverflow.com/questions/6195167/problems-trying-to-attach-a-new-ef4-entity-to-objectcontext-while-its-entity-coll\">problem这里。

我试图使用solution朱莉勒曼给了我几个月前。我目前使用以下生成一个新的游戏实体pre-附在我的ObjectContext:

 游戏游戏= _gameRepository.GetGame(formData.GameID);
AutoMapper.Mapper.Map&LT; AdminGameEditModel,游戏&GT;(FORMDATA,游戏);

在库中,我尝试将游戏安装到与它的状态设置为新增的OC像她建议通过执行以下操作:

 公共游戏GetGame(INT ID)
{
    如果(ID大于0)
    {
        返回_siteDB.Games.Include(体裁)包括(平台)的SingleOrDefault(G =&GT; g.GameID == ID)。;
    }
    其他
    {
        游戏游戏= _siteDB.Games.CreateObject();
        _siteDB.Games.AddObject(游戏);
        返回游戏;
    }
}

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

 公共AdminController(IArticleRepository articleRepository,IGameRepository gameRepository,INewsRepository newsRepository)
{
    _articleRepository = articleRepository;
    _gameRepository = gameRepository;
    _newsRepository = newsRepository;    Mapper.CreateMap&LT; AdminGameEditModel,游戏&GT;()
        .BeforeMap((S,D)=&GT;
        {
            如果(d.Platforms.Count大于0)
            {
                平台[] =现有d.Platforms.ToArray();                的foreach(现有VAR高原)
                {
                    d.Platforms.Remove(高原)
                }
            }            的foreach(在s.PlatformIDs VAR platId)
            {
                平台newPlat = _gameRepository.GetPlatform(platId);
                d.Platforms.Add(newPlat);
            }
        })
        .ForMember(DEST =&GT; dest.BoxArtPath,选择=&GT; opt.Ignore())
        .ForMember(DEST =&GT; dest.IndexImagePath,选择=&GT; opt.Ignore())
        .ForMember(DEST =&GT; dest.Cons,选择=&GT; opt.MapFrom(SRC =&GT;的string.join(|,src.Cons)))
        .ForMember(DEST =&GT; dest.Pros,选择=&GT; opt.MapFrom(SRC =&GT;的string.join(|,src.Pros)))
        .ForMember(DEST =&GT; dest.LastModified,选择=&GT; opt.UseValue(DateTime.Now))
        .ForMember(DEST =&GT; dest.Platforms,选择=&GT; opt.Ignore());
}

正如你所看到的,_gameRepository应该是一样的,因为它的控制器上创建建设。这,反过来,这意味着在_gameRepository的OC应是既游戏和平台一样。然而,随着既然如此,我仍然得到它规定一个例外:


  

两个对象之间的关系不能被限定,因为它们连接到不同的ObjectContext对象


扭曲的东西肯定是怎么回事,这就是为什么我想知道如果我能真正跟踪的ObjectContext的实体实际上是联系在一起的。他们都应该被连接到相同的OC,但例外另有主张。

或许这是与我使用Ninject(香草版本的的MVC的定制版本)注入控制器的存储库。无论什么问题是,它几乎似乎是显而易见的。任何帮助将极大地 AP preciated。

编辑:库的ObjectContext的:

 公共类HGGameRepository:IGameRepository
{
    私人HGEntities _siteDB =新HGEntities();    //类code休息
}

Ninject绑定:

 私有类HandiGamerServices:NinjectModule
{
    公共覆盖无效负载()
    {
        绑定&所述; IArticleRepository方式&gt;()到&lt; HGArticleRepository方式&gt;()InRequestScope();
        绑定&所述; IGameRepository方式&gt;()到&lt; HGGameRepository方式&gt;()InRequestScope();
        绑定&所述; INewsRepository方式&gt;()到&lt; HGNewsRepository方式&gt;()InRequestScope();
        绑定&LT; ErrorController&GT;()ToSelf()InRequestScope()。
    }
}


解决方案

您可以询问ObjectContext的,如果它有通过一定对象的引用:

  ObjectStateEntry OSE;
布尔isInContext = someContext.ObjectStateManager.TryGetObjectStateEntry(someObject,出OSE);

This is in continuation with my problem here.

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

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

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:

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

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.

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.

EDIT: Repository's ObjectContext:

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

    // rest of class code
}

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

解决方案

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天全站免登陆