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

  • SEARCH
  • Home
  • 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 8065525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:41:33+00:00 2026-06-05T11:41:33+00:00

I have a problem with foreign keys in EF 4.0. I have a little

  • 0

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.

  • 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-06-05T11:41:35+00:00Added an answer on June 5, 2026 at 11:41 am

    Strange things here

    Character c = new Character();
    c.Player.Id = idPlayer;//But instance c has no Player (well, I don't know the constructor of Character, but I may imagine there's no new Player() inside)
    

    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.

    Character c = new Character{
       Player =  model.GetPlayerByid(idPlayer);//or something like that
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some problem with MySQL Workbench in that I sometimes can't set foreign
I have a problem when trying to update foreign keys. Using the merge function
In this question I was having problem with saving objects that had foreign keys
I have a problem in designing a database for a business. I have some
I have written some SQL code to create five tables with several relations(foreign keys).
I have a problem with Pocos and nullable foreign keys . I have 2
I have a problem where i need a cascade on multiple foreign keys pointing
I have to make one of the foreign keys unique. The problem is, I
I have a problem when using foreign keys and primary keys in MySQL datable.
I have a question about a ForeignKey reference problem with django. This is a

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.