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 6070031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:53:14+00:00 2026-05-23T09:53:14+00:00

I’ve been trying to get EF4 to work with a MVC 2 project for

  • 0

I’ve been trying to get EF4 to work with a MVC 2 project for about six months now. It’s been an exercise in torture. Trying to do anything non-trivial requires juggling EntityKeys, doing the ObjectContext Attach/Detach dance, and, really, trying to divine the necessary black magic to get it to work based on vague, mostly unhelpful Exception messages. To say it’s been frustrating is a vast, vast understatement.

So, venting aside, here’s my problem: I have a somewhat complex model I’m trying to create/edit via forms. While most of the data is simple scalars, there is some many-to-many data in there as well. The site is going to be (if I ever finish…) a video game review site. As such, the many-to-many relationship is one between Games and the Platforms they’re available on (XBox 360, PS3, etc.). The Platforms are selected via checkboxes, with each checkbox being associated with a particular Platform id.

In order to try to do things the right way, I’m binding to an edit model before transferring the data to actual Game and Platform entities. Once the data is transferred, I call my Save method on my repository, which is really just a wrapper for my ObjectContext.

public void SaveGame(Game game)
{
    _siteDB.Games.Attach(game);

    if (game.GameID > 0)
    {
        _siteDB.ObjectStateManager.ChangeObjectState(game, System.Data.EntityState.Modified);
    }
    else
    {
        _siteDB.ObjectStateManager.ChangeObjectState(game, System.Data.EntityState.Added);
    }

    _siteDB.SaveChanges();
}

Right now, I’m running into errors with my Attach statement. Left where it is, I get an exception telling me that the ObjectContext can’t track two Entities with the same EntityKey. If I remove it, I get an exception telling me that the Game needs an EntityKey. I’ve tried using ApplyCurrentValues, but then I run into similar problems trying to Add the Platforms.

Ultimately, it seems as though there’s one particular pattern to use in order to get something like this to work. I just can seem to discover what it is, and it’s been immensely frustrating. I just can’t see a solution which doesn’t require me blowing up the separation between my UI and back end.

Can anyone point me in the right direction? Are there any MVC/EF4 tutorials that deal with things more complicated than the famed demoware examples?


EDIT: Made some progress with the following:

public void SaveGame(Game game)
{
    if (game.GameID > 0)
    {
        var editedGame = this.GetGame(game.GameID);

        var eSet = editedGame.EntityKey.EntitySetName;
        _siteDB.ApplyCurrentValues(eSet, game);

        var existingPlats = editedGame.Platforms.ToArray();

        foreach (var plat in existingPlats)
        {
            editedGame.Platforms.Remove(plat);
        }

        var newPlats = game.Platforms.ToArray();

        foreach (var nPlat in newPlats)
        {
            editedGame.Platforms.Add(nPlat);
        }

        game = null;

        _siteDB.ObjectStateManager.ChangeObjectState(editedGame, System.Data.EntityState.Modified);
    }
    else
    {
        _siteDB.Games.AddObject(game);
    }

    _siteDB.SaveChanges();
}

My only problem now is that its adding game to the ObjectContext even though I’m setting it to null. So, my existing entity updates fine, but I’m getting a new, mirrored entity inserted into the db as well.

  • 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-23T09:53:15+00:00Added an answer on May 23, 2026 at 9:53 am

    The problem is that you already have a Game in the context with the same ID. You can’t have two objects with the same ID.

    How you got into that state in the first place is a function of code you don’t show. But let’s talk about solving your immediate problem. There are essentially two approaches you could take:

    1. Detach the existing object, or
    2. Keep the existing object, and copy the values over from the new object (the one you pass as an argument).

    You can pick the one which works for you, but the basic problem here is that you have two different instances in memory with the same GameID value, and that’s bad.

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

Sidebar

Related Questions

I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a text area in my form which accepts all possible characters from
I am writing an app with both english and french support. The app requests
I'm parsing an XML file, the creators of it stuck in a bunch social
I am using Paperclip to handle profile photo uploads in my app. They upload

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.