Entity Framework 4. Join Tables. Entity Framework perfoming an Insert when no action is required
I have a database setup with the following: -
Person Table
Hobby Table
Game Table
GameInfo Table
Person [1 - M] Hobby [1 - M] Game [M - 1] GameInfo
Game
is just a join from Hobby
to GameInfo
I am having an issue whereby I would fetch Person
which has a Collection<Game>
and add to this collection (i.e. I am just updating links, not wanting to insert new GameInfo
).
Upon Calling SaveChanges()
EntityFramework will insert the links AS WELL AS inserting new GameInfo
, which is not my desired result.
I have looked at Entry().State etc but the problem is where I am handling the updates of my Person
is outside of the context.
I am basically fetching a Person
creating a new Game
with Ids that I know exist already and then calling the SaveChanges()
and would expect that it would just insert into the Game
Table, not the GameInfo table
EDIT 1: Code Sample - sort of
public void Save(Profile profile)
{
using (GDContext context = GetContext())
{
DataProfile dataProfile = context.Profiles.Single(u => u.ProfileId == profile.Id);
ProfileHandler.HandleDataModelChanges(dataProfile, profile);
context.SaveChanges();
}
}
public override void HandleDataModelChanges(DataProfile dataModel, Profile model)
{
dataModel.ProfileId = model.Id;
dataModel.FirstName = model.FirstName;
dataModel.LastName = model.LastName;
dataModel.DateOfBirth = model.DateOfBirth;
dataModel.Email = model.Email;
foreach(var hobby in model.Hobbies)
{
DataHobby dataHobby = dataModel.Hobbies.SingleOrDefault(p => p.HobbyId == hobby.HobbyId);
if (dataHobby == null)
{
dataHobby = new DataHobby();
}
HobbyHandler.HandleDataModelChanges(dataHobby, hobby);
}
}
public override void HandleDataModelChanges(DataHobby dataModel, Hobby model)
{
dataModel.HobbyId = model.Id;
HandleGames(dataModel, model);
HandleCrafts(dataModel, model);
HandleCollections(dataModel, model);
}
private void HandleGames(DataHobby dataModel, Hobby model)
{
IEnumerable<DataGame> gamesToRemove = dataModel.Games.Where(g => !model.Games.Any(ds => ds.Id == g.GameId)).ToArray();
foreach (var game in gamesTo开发者_运维技巧Remove)
{
dataModel.Games.Remove(game);
}
foreach (var game in model.Games)
{
if (!dataModel.Games.Any(e => e.GameId == game.Id))
{
DataGame dataGame = new DataGame();
dataGame.GameId = game.Id;
dataGame.GameName = game.Name;
dataModel.Games.Add(dataGame);
}
}
}
EDIT 2 - Context configuration
this.Configuration.LazyLoadingEnabled = false;
this.Configuration.AutoDetectChangesEnabled = true;
public GameInfoConfiguration()
{
HasKey(x => x.GameId);
ToTable("GameData");
}
public PersonConfiguration()
{
HasKey(x => x.PersonId);
ToTable("Person");
}
public HobbyConfiguration()
{
HasKey(x => x.HobbyId);
HasRequired(x => x.Person).WithMany(x => x.Hobbies);
HasMany(x => x.Games).WithMany(g => g.Hobbies).Map(x => x.MapLeftKey("HobbieId").MapRightKey("GameId").ToTable("PersonGame"));
ToTable("HobbyGame");
}
Well I still don't see where you work with GameInfo
- your description absolutely doesn't correspond with your code. By looking at your code I guess the problem will be in the snippet like this:
foreach (var game in model.Games)
{
if (!dataModel.Games.Any(e => e.GameId == game.Id))
{
DataGame dataGame = new DataGame();
dataGame.GameId = game.Id;
dataGame.GameName = game.Name;
dataModel.Games.Add(dataGame);
}
}
This will always insert a new Game
- you told EF to insert a new Game
. If you want to add existing Game
you must do:
foreach (var game in model.Games)
{
if (!dataModel.Games.Any(e => e.GameId == game.Id))
{
DataGame dataGame = new DataGame();
dataGame.GameId = game.Id;
dataGame.GameName = game.Name;
context.Games.Attach(dataGame); // Now the context knows that it is not a new entity
dataModel.Games.Add(dataGame);
}
}
I think the mistake I have made here is that I am dealing with DataGame
when really what I should be dealing with is a POCO class to represent the "join" between DataGame
and Hobby
Like a HobbyGame
POCO.
精彩评论