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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:33:34+00:00 2026-06-09T04:33:34+00:00

I have some POCO objects that look something like this: public class Foo {

  • 0

I have some POCO objects that look something like this:

public class Foo
{
    public int Id { get; set; }
    public string FooProperty { get; set; }
    public int BarId { get; set; }
    public virtual Bar Bar { get; set; }
}
public class Bar
{
    public int Id { get; set; }
    public string BarProperty { get; set; }
    public int FooId { get; set; }
    public virtual Foo Foo { get; set; }
}

Each Foo object has exactly one Bar (and vice-versa).

Now I want to create a new pair of Foo/Bar objects. So I do this (and this is where I suspect I’m going wrong):

var foo = new Foo() { FooProperty = "hello" };

dbContext.Foos.Add(foo);

var bar = new Bar() { BarProperty = "world" };

foo.Bar = bar;

dbContext.SaveChanges();

As you can probably tell, I’m hoping that because I “added” foo, then bar will also be added because it’s part of the same object graph, but no: it isn’t added – and nor is the FooId of the Bar object updated after the call to SaveChanges (though the Id of the Foo object is updated).

So, my guess is that this behaviour is because I’m dealing with POCOs and not EF proxy objects, and so there’s no “plumbing” to make this work. I could fetch the Id from the Foo object and manually stash it in the Bar object (and vice-versa) and make some more calls to SaveChanges, but obviously that’s not the right way to go.

So, presumably I need to create EF proxy objects rather than bare POCO objects. What’s the best way to do that?

  • 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-09T04:33:35+00:00Added an answer on June 9, 2026 at 4:33 am

    If the entities meet the requirements for creating proxy objects, you can make this work by calling the create function from the context itself:

        var foo = dbContext.Foos.Create();
        dbContext.Foos.Add(foo);
        var bar = dbContext.Bars.Create();
        foo.Bar = bar;
        dbContext.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some POCO objects that are set up for use with Entity Framework
I have some text lines like that : vt_wildshade2^508^508 vt_ailleurs2^1188^1188 ... vt_high2^13652^13652 Is it
I have been using LINQ to query my POCO objects for some time, but
My application I have an application design that looks like this: web application layer
We have an object that derives from DependencyObject, and implements some DependencyProperties. Basically something
I have a IQueryable<SomePOCO> (a LINQ-Entities query, if that matters): public class SomePOCO {
I have read some articles on POCO in the enttity framework but still don't
I have some arbitrary pixel data that I want to save as a PNG.
I have some tables roughly like so: Client: id name Employee id name Email
I have some code that will change the background color of a specific label

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.