0

这是我的问题的延续。

我正在尝试使用Julie Lerman 几个月前给我的解决方案。我目前正在使用以下内容生成预先附加到我的 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();
    }
}
4

2 回答 2

2

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

ObjectStateEntry ose;
bool isInContext = someContext.ObjectStateManager.TryGetObjectStateEntry(someObject, out ose);
于 2011-06-03T21:22:01.290 回答
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();

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

于 2011-06-03T01:46:49.087 回答