0

This is somewhat complicated to explain, so please bear with me.

I have an ASP.NET MVC 2 project that is slowly killing me in which I'm trying to take form data and translate it into entities to create or update, depending on the context of the situation. The most relevant parts (pseudo-code):

Entity Game
    Scalar properties
    EntityCollection<Platform> Platforms

And the basic workflow is:

Form data -> model bound to a DTO -> mapping the DTO to an EF4 entity with AutoMapper.

It all works well with one exception - I need to create or update the Game entity's Platforms EntityCollection with the raw integer index data contained in the DTO. So, here's what I've been trying, which does not work:

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)
            {
                var newPlat = _gameRepository.GetPlatform(platId);

                d.Platforms.Add(newPlat); // <-- where it chokes
            }
        })
        .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());
}

To be specific, I'm getting an exception at the line I highlighted above which says:

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

Some research tells me that this is to be expected, as my fresh Game object has no ObjectContext that it's associated with, and a null context is considered to be a separate context. See this brief explanation from Julie Lerman for more.

Okay, so being the intrepid person I am, I figured I would simply be able to register my game with the ObjectContext and everything would be fixed. So, I tried:

Game game = new Game();
_gameRepository.RegisterGame(game);

Where RegisterGame is simply:

public void RegisterGame(Game game)
{
    _siteDB.Games.AddObject(game);
}

Unfortunately, that did not work. The same exception is being thrown at the same point.

So, it looks like I'll have to add each Platform's EntityKey to my EntityCollection. Only problem is, I'm not sure how to do it.

So, any ideas?


EDIT: Progress of a sort. I tried adding just the EntityKeys of the Platform entities, like so:

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)
        {
            var newPlat = _gameRepository.GetPlatform(platId);

            d.Platforms.Add(new Platform { EntityKey = newPlat.EntityKey });
        }
    })

And it removes the 'two different ObjectContexts' exception. The problem is that I'm getting the following exception if I try to add or attach the new Game entity to my context:

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

Unfortunately, the exception doesn't specify which object it is, but I'm betting it's one of the Platform objects, since they already exist and I'm merely trying to forge a new relationship between the two.

So, the question remains: how the hell does one make a new entity and populate its EntityCollection<> property with existing entities? I can't be the only person who ever wanted to create a new entity and create new many-to-many relationships between it and existing entities, right?

Major Productions
  • 5,914
  • 13
  • 70
  • 149
  • Hey, VT here ;) In case you check this first, I'll quote what I said in the thread: Is your "GetPlatform" method using the same context as your "RegisterGame" method was using? Double check the methods, and that they're accessing the *exact* same instance of Context (not a clone, not a separate instance, but the exact same one). Oh, and make sure you are Adding that new Game object to the context (i.e. don't throw out the change you made to add it to the context), you definitely want it to be associated with the context. – EdgarVerona Jun 03 '11 at 20:40
  • Yeah, that's the next step. It LOOKS like it's the same context, as both methods are accessing the same repo, but the exception is making me wonder. I'm in the process of learning how to create one context and share it with all my repos with Ninject. – Major Productions Jun 03 '11 at 21:22
  • Ah, but are the internals of the specific methods also using the same instance of the Context? I could see a scenario where one might've accidentally - for example - created a new context within the "GetPlatform" method. Definitely check it just to be sure. The other thing to check would be that they're not using separate instances of the repository itself. Both are definitely worth checking! – EdgarVerona Jun 03 '11 at 21:35
  • Definitely going to check it when I get the chance, although I have the sneaking suspicion that if I do have a duplicate context, it's happening with AutoMapper itself. Ideally, I'd create one context, then inject it into my repos, which themselves would be injected into my controllers. I'm in the process of figuring that out, too, if you want to take a gander: http://stackoverflow.com/questions/6231851/ef4-0-repositories-and-ninject-2 – Major Productions Jun 03 '11 at 22:38

3 Answers3

0

Just try this:

foreach (var platId in s.PlatformIDs)
{
    Platfrom p = new Platform { Id = platId };
    context.Attach(p) 
    d.Platforms.Add(p);
}

You don't have to load the entity to make a relation. You just need a dummy with correct Id (which you already have). The dummy must be attached to the context.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Running into exceptions trying your solution. If I use _siteDB.Attach(plat), I get an exception telling me it cannot attach an entity with a null entitykey. If I use _siteDB.Platforms.Attach(plat), I get an exception telling me an object with the same key already exists within the context. – Major Productions Jun 02 '11 at 18:39
0

I got it to work by using Julie Lerman's original solution. I didn't have to detach/re-attach my platforms with my original, pre-DTO solution, so I thought I didn't need to here. In any event, it looks like I need to do more research on how to handle the ObjectContext.

Community
  • 1
  • 1
Major Productions
  • 5,914
  • 13
  • 70
  • 149
0

I had originally solved this (and other problems I was having with EF4 many-to-many graphs in conjunction with AutoMapper) by simply creating my own cludgy static mapper. It's ugly, not at all abstract, but it works. With the release of AutoMapper 2.0, I decided to see if I could finally get everything to work the way I want.

Amazingly, I got the same "different ObjectContexts" exception in the AutoMapper code as I did originally. It works without an exception in my own method, but not in AutoMapper. Odd, seeing as the code is essentially the same.

AutoMapper map:

Mapper.CreateMap<AdminGameViewModel, Game>()
    .BeforeMap((s, d) =>
    {
        if (d.Platforms != null && d.Platforms.Count > 0)
        {
            var oldPlats = d.Platforms.ToArray();

            foreach (var oldPlat in oldPlats)
            {
                d.Platforms.Remove(oldPlat);
            }
        }

        foreach (var platId in s.PlatformIDs)
        {
            var plat = _gameRepository.GetPlatform(platId);
            d.Platforms.Add(plat);
        }
    })
    .ForMember(dest => dest.Platforms, opt => opt.Ignore())
    .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));

My own mapper:

public static Game MapFromEditModelToGame(IGameRepository repo, AdminGameViewModel formData, Game newGame)
{
    newGame.GameID = formData.GameID;
    newGame.GameTitle = formData.GameTitle;
    newGame.GenreID = formData.GenreID;
    newGame.LastModified = DateTime.Now;
    newGame.ReviewScore = (short)formData.ReviewScore;
    newGame.ReviewText = formData.ReviewText;
    newGame.Cons = String.Join("|", formData.Cons);
    newGame.Pros = String.Join("|", formData.Pros);
    newGame.Slug = formData.Slug;

    if (newGame.Platforms != null && newGame.Platforms.Count > 0)
    {
        var oldPlats = newGame.Platforms.ToArray();

        foreach (var oldPlat in oldPlats)
        {
            newGame.Platforms.Remove(oldPlat);
        }
    }

    foreach (var platId in formData.PlatformIDs)
    {
        var plat = repo.GetPlatform(platId);
        newGame.Platforms.Add(plat);
    }

    return newGame;
}

I can only guess that there's some odd scope issue in play, but I think it's an interesting (read: frustrating) problem. Can't tell if it's due to EF4 itself, or my attempt to use it with AutoMapper.

Major Productions
  • 5,914
  • 13
  • 70
  • 149