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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:54:41+00:00 2026-05-26T17:54:41+00:00

I have a POCO A entity referencing a B entity, let’s say b .

  • 0

I have a POCO A entity referencing a B entity, let’s say b. I want A to reference a different exising B entity, let’s say bb.

These steps:

var b = // get existing b from somewhere out-of-context
var a = new A { B = b }
dbcontext.Set<B>.Attach(a.B);
dbcontext.Set<A>.Add(a);
context.SaveChanges();

generate an insert statement for a as expected with the B_ID foreign key properly set to the primary key ID of b. These subsequent steps:

var bb = // get existing bb from somewhere out-of-context
a.B = bb;
differentdbcontext.Set<B>.Attach(a.B);
differentdbcontext.Set<A>.Attach(a);
differentdbcontext.Entry(a).State = EntityState.Modified;
differentdbcontext.SaveChanges();

result in no change to the persisted data. The update statement does not include the set B_ID = ... as expected.

I’m doing something simple wrong as I’ve had other scenarios like this working before.

  • 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-05-26T17:54:41+00:00Added an answer on May 26, 2026 at 5:54 pm

    Setting the state to Modified only has an effect on scalar properties but not on your navigation properties. I assume that B_ID is not a property in your model but only the foreign key column in the database not being exposed to your model.

    In this case you can update the relationship only by leveraging the automatic change detection of Entity Framework. One approach – and I would call this the standard approach – is to load the original A including the original B from the database, set a.B to your new bb and then save the changes:

    var bb = // get existing bb from somewhere out-of-context
    
    differentdbcontext.Set<B>().Attach(bb);
    differentdbcontext.Set<A>().Include(x => x.B).Single(x => x.Id == a.Id);
    
    a.B = bb;
    
    differentdbcontext.SaveChanges();
    

    If you don’t want to load the original from the DB some trick programming is required:

    var bb = // get existing bb from somewhere out-of-context
    
    if (  (a.B == null && bb != null) 
       || (a.B != null && bb == null)
       || (a.B != null && bb != null && a.B.Id != bb.Id)) //take care not to attach
                                                          //two objects with same key
    {
        if (bb != null)
            differentdbcontext.Set<B>().Attach(bb);
        differentdbcontext.Set<A>().Attach(a);
        a.B = bb; // EF will detect this change
    }
    else if (a.B == null && bb == null)
    {
        // create a dummy a.B
        a.B = new B(); // it doesn't matter which Id
        differentdbcontext.Set<A>().Attach(a);
        a.B = bb; // = null -> EF will detect a change
    }
    
    differentdbcontext.SaveChanges();
    

    Or similar. The idea is to change the reference after attaching the objects so that change detection will send an update of the FK column to the database.

    Exposing foreign keys as properties into your model will make this situation much easier. Setting the state to Modified would work then because FK properties are scalar.

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

Sidebar

Related Questions

Possible Duplicate: Comparing object properties in c# Let's say I have a POCO: public
I have a 'Customer' POCO entity within my Entity Framework 4 project. I want
I have a database, and I have entity POCO's, and all I want to
I have a proxy entity but I want to save the poco entity to
I have converted an Entity framework project to use POCO objects by removing the
I just installed the POCO Template for EF4. I have a single entity in
I have a database table and a corresponding entity class (POCO with change tracking
When I add new item and choose the ADO.NET POCO Entity Generator, I get
Say I have a POCO that stores a date range as follows: public class
I have an Entity Framework POCO class that is generated by a T4 Template.

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.