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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:52:33+00:00 2026-06-17T09:52:33+00:00

First of all I am not an expert on Entity Framework. And I have

  • 0

First of all I am not an expert on Entity Framework.

And I have seen some piece of code in one project’s Repositories, which

  • NULLs the EntityReference properties on the entity
  • And keeps that EntityReference’s ID

before Saving changes of this entity.

For example it does this setting before saving Foo entity:

if (foo.Bar !=null)
{
  foo.BarID = foo.Bar.ID;
  foo.Bar = null;
}

Can this be an EntityFramework related requirement to null the EntityReference properties but using their IDs only in order to Save ?

  • 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-17T09:52:34+00:00Added an answer on June 17, 2026 at 9:52 am

    No, normally that’s not a requirement. When working with attached entities for example you can change the FK property or the reference, both would work:

    var foo = context.Foos.Include("Bar").Single(f => f.ID == 1);
    

    Then…

    foo.BarID = 5;
    context.SaveChanges();
    

    …would work and…

    var newBar = new Bar { ID = 5 };
    context.Bars.Attach(newBar);
    foo.Bar = newBar;
    context.SaveChanges();
    

    …will work as well. No need to null out the reference.

    The code in your question would possibly make sense if

    • the foo – foo.Bar relationship is in an inconsistent/contradicting state
    • and you know what is wrong

    For example:

    var foo = new Foo { ID = 1 };
    
    foo.BarID = 4;
    foo.Bar = new Bar { ID = 5 };
    
    repo.UpdateFoo(foo);
    

    And UpdateFoo is:

    public void UpdateFoo(Foo foo)
    {
        context.Foos.Attach(foo); // will throw an exception
        context.Entry(foo).State = EntityState.Modified;
        context.SaveChanges();
    }
    

    The Attach line would throw an exception because the FK value BarID is 4 but the referenced foo.Bar is the Bar number 5. EF does not know what is valid and will refuse to write an Update.

    You could fix this problem now be adding the code in your question into the UpdateFoo method:

    public void UpdateFoo(Foo foo)
    {
        if (foo.Bar !=null)
        {
            foo.BarID = foo.Bar.ID;
            foo.Bar = null; // this line is not really necessary
        }
    
        context.Foos.Attach(foo); // no  exception anymore
        context.Entry(foo).State = EntityState.Modified;
        context.SaveChanges();
    }
    

    But this relies on the assumption that foo.Bar (with ID = 5) is valid and foo.BarID (with ID = 4) is invalid.

    Generally this isn’t necessarily true, it could be just the other way around. In my opinion this snippet is a code smell which tries to fix an inconsistency in the repository layer that has actually been created in another layer (business layer or so).

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

Sidebar

Related Questions

First of all, I'm not a javascript expert. I'm going crazy on trying to
First of all, I am not the one who is writing the regexps, so
My first time using the Entity Framework so I'm not sure if I'm doing
First of all, I am not en expert at programming, so please explain things
First of all: I am not able to find out the proper Title of
first of all: this is not the same as this . The ModelBackend has
First of all I'm not sure this is even possible, however I need to
First of all I am not a designer. so this question is may be
First of all I'm not sure if .exe window is the proper term. It's
first of all I'm really not really friendly with ruby or with rails, so

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.