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开发者_如何学Python 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.
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:
- Detach the existing object, or
- 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.
精彩评论