I have a problem with foreign keys in EF 4.0.
I have a little game with a player. This Player can have some characters in the game.
So, when I want to add a character to database, I must set the foreign key “PlayerId”.
This is my code, but kill when adding object in context model :
using (DatabaseModelContainer model = new DatabaseModelContainer())
{
Character c = new Character();
c.Player.Id = idPlayer;
model.CharacterJeu.AddObject(c);
model.SaveChanges();
}
Thank’s for answers.
Strange things here
By the way, with the model you seem to have, you don’t have to manage the FK as you do. You have to manage Reference (navigation) properties.
You can do it that way.