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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:32:16+00:00 2026-06-10T22:32:16+00:00

I see a lot of examples on using EF code first with POCOs that

  • 0

I see a lot of examples on using EF code first with POCOs that show something like this:

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public int BlogId { get; set; }
    public virtual Blog Blog { get; set; }
}

Now, look at the Blog property. Should not it be like this instead:

private Blog blog;
public virtual Blog Blog 
{ 
    get
    {
        return blog;
    } 
    set
    {
        blog = value;
        if (blog != null)
        {
            BlogId = blog.BlogId;
        }
    } 
}

I mean, since you already are “polluting” your model with the foreign key, should not you at least keep it in sync with the reference? Or you should not rely on BlogId when reading data anyway (eg. like you want to know if a specific BlogId is on a list). Or maybe there is a magic property on DbContext (like KeepForeingKeysPropertiesSyncronizedWithReferences) that does that to me and I am the only sad programmer that are woried about this? Or am I paranoid? (also, sorry for my poor english)

EDIT
Sorry for that – this really was a stupid question. Stefan is right, EF really does this for you. I wasn’t seen this because the references I was passing was loaded with AsNoTracking(). Only in this condition you will have a reference with ID and the foreing key field will be 0. As long as you pass a reference that is already on the context, it should work.

  • 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-10T22:32:18+00:00Added an answer on June 10, 2026 at 10:32 pm

    You don’t need to do this per se, EF does this for you.
    If you set the independent association property, EF will synchronize the foreign key property to reflect your change and vice versa. It doesn’t happen immediately automatically mind you, I think this happens after calling SaveChanges but I’m not sure.
    If you want to know why some people use the foreign key property: it’s more convenient in detached apps like web apps. When you need to scale your app, it can also help to improve performance (read this in a recent MS blog post somewhere).
    Basically, you only need to do this yourself if you’re mixing and matching; when in one method you’re using the independent association property and in another you’re using the foreign key property.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I see a lot of people are using code under viewDidLoad to be able
Profiling my code, i see a lot of cache misses and would like to
After reading through many code examples for Google Guice, I see a lot of
I see a lot of examples of how to use StructureMap in a asp.net
I've see a lot of MVC examples where domain-objects are passed directly to views,
I see there are lot's of examples in Ext JS where instead of actually
I see a lot of this: 06-19 17:29:11.911: DEBUG/dalvikvm(10028): GC_FOR_MALLOC freed 729K, 54% free
I'm reading through someone else's code, and I see a lot of instances of
Why don't I ever see the following in Python code? class A: def __init__(self,
This is my first post, so hope I haven't missed anything important. I'm doing

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.