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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:58:23+00:00 2026-06-01T23:58:23+00:00

I am having trouble with foreign key relationships in the entity framework. I have

  • 0

I am having trouble with foreign key relationships in the entity framework. I have two tables: Persons and Countries. Persons has a foreign key column to CountryId.

As the Countries table rarely changes, I want to fetch its data only once, dispose the DatabaseContext, and keep the list of Countries cached somewhere. This is where I am running into problems.

The entity framework seems to want you to open a database context, add/edit rows as needed, then close the database context. If you open, fetch data, close; and then later open, save data, close; it has trouble.

So my POCO objects look like this:

public class Country {
    public int CountryId {get; set; }
    public String Name {get; set; }
}

public Person {
    public int PersonId {get; set; }
    public virtual Country Country {get; set; }
}

Then, I try to create a new person like this:

Country[] countries;
using (var dt = new DatabaseContext())
{
    countries= dt.Countries.ToArray();
}

Person person = new Person();
person.Country = countries[0];
using (var dt = new DatabaseContext()) { 
    dt.Entities.Add(person);
    dt.SaveChanges();
}

On save, the entity framework creates a new row in the Countries table with the same name as countries[0], but a new, incremented ID. This is obviously not the desired outcome – the person should have its Country_CountryId field set to the id of countries[0], and a new row should not be created.

How do I solve this? I think one solution would be to force the entity framework to not create a new row when it is given an object that already has its primary key set. Is there a way to do this?

  • 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-01T23:58:24+00:00Added an answer on June 1, 2026 at 11:58 pm

    I wonder if you at least search little bit on Internet before you put quite big effort to describe the issue because this is very common problem asked every few days.

    Add method adds all entities in the entity graph. So if you connect country to person and country is not attached to current context, calling Add on the person will mark both person and country as new entities for insertion. If you don’t want country to be inserted you must tell EF that country is not a new entity:

    Person person = new Person();
    person.Country = countries[0];
    using (var dt = new DatabaseContext()) { 
        dt.Entities.Add(person);
        dt.Entry(person.Country).State = EntityState.Modified;
        dt.SaveChanges();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having Trouble with Entity Framework. I have been populating EntityReferences with an EntityKey inorder
I am having trouble with SQL Server creating Foreign Key constraints when using Entity
I'm having trouble mapping the following in NHibernate: I have two tables (these are
I am having trouble with foreign keys Dynamic Data and entity framework 4.0. It
I'm having trouble figuring out how to create a foreign key constraint. My data
We have a MySQL database that has two tables, Warehouse and Crate Warehouse: WarehouseID
Having trouble linking the Stomp.framework into an iPhone SDK application. http://code.google.com/p/stompframework/ I follow the
Having trouble with a mysql grant statement. I want a user who has: readonly
I am having trouble setting up jpa mappings for some entities. I have a
Morning, I'm having some trouble creating tables/loading fixtures. (symfony 1.4.6 with the bundled Doctrine

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.