Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5990017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:11:01+00:00 2026-05-22T23:11:01+00:00

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

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:11:02+00:00Added an answer on May 22, 2026 at 11:11 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is somewhat asp.net MVC related only for example purposes but I was hoping
This is somewhat of a follow-up to an answer here . I have a
This is somewhat of a follow-up question to this question . Suppose I have
I know that this is somewhat subjective, but I wonder if there is a
Although somewhat related to this question , I have what I think is a
I have a central hg repository with multiple subprojects in it. Somewhat like this:
I know this is somewhat of a server question, but I wanted to ask
This is a somewhat low-level question. In x86 assembly there are two SSE instructions:
This is a somewhat bizarre question. My objectives are to understand the language design
[This question is somewhat related to this question , but the answers are not...]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.