C# EF4.0-有没有办法在调试期间查看哪些实体附加到哪个ObjectContext?

C# EF4.0-有没有办法在调试期间查看哪些实体附加到哪个ObjectContext?,c#,asp.net-mvc-2,entity-framework-4,ninject,objectcontext,C#,Asp.net Mvc 2,Entity Framework 4,Ninject,Objectcontext,这是我的计划的延续 我在试着用这个。我目前正在使用以下方法生成预先附加到ObjectContext的新游戏实体: Game game = _gameRepository.GetGame(formData.GameID); AutoMapper.Mapper.Map<AdminGameEditModel, Game>(formData, game); public class HGGameRepository : IGameRepository { private HGEnt

这是我的计划的延续

我在试着用这个。我目前正在使用以下方法生成预先附加到ObjectContext的新游戏实体:

Game game = _gameRepository.GetGame(formData.GameID);
AutoMapper.Mapper.Map<AdminGameEditModel, Game>(formData, game);
public class HGGameRepository : IGameRepository
{
    private HGEntities _siteDB = new HGEntities();

    // rest of class code
}
现在,为了清楚起见,这里是我的控制器的全部构造函数:

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());
}
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();
    }
}
私有类HandiGamerServices:ninject模块
{
公共覆盖无效负载()
{
Bind().To().InRequestScope();
Bind().To().InRequestScope();
Bind().To().InRequestScope();
Bind().ToSelf().InRequestScope();
}
}

问题在于

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();
Bind().ToSelf().InRequestScope();

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

问题在于

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();
Bind().ToSelf().InRequestScope();

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

您可以通过以下方式询问ObjectContext是否有对特定对象的引用:

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

您可以通过以下方式询问ObjectContext是否有对特定对象的引用:

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

没有这样的运气,得到相同的异常。要让它工作,还必须在控制器构造函数中传入HGEntities,如下所示:public AdminController(HGEntities entities,IArticlerRepository articleRepository等)没有这样的运气,得到相同的异常。要让它工作,您还必须在控制器构造函数中传入HGEntities,如下所示:public AdminController(HGEntities entities、IArticleRepository articleRepository等)