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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:21:35+00:00 2026-05-29T09:21:35+00:00

I’m experiencing a problem in breaking a relationship bteween two entities. Let’s say we

  • 0

I’m experiencing a problem in breaking a relationship bteween two entities. Let’s say we have a Child entity which belongs to one Parent, a Parent has many Child entities. Now in my front end a user can select a radio button to choose which Parent the Child belongs to or they can choose “None”. The trouble I’m having is that when I edit a Child record and select “None”, it doesn’t seem to break an existing relationship.

In my business logic I am doing the following:

child.Parent = parentRepository.Find(command.ParentID);

The repository method returns null if no record matches the passed in ID, and selecting “None” gives an ID of 0 yet when I debug and step over this line, Child.Parent still has a reference to the previously chosen Parent entity. In fact, even explicitly setting Child.Parent = null; doesn’t seem to break the link.

To add to my confusion, this isn’t consistant behaviour. Every now and then the same code does break the relationship and set Child.Parent to null! Can someone please point me in the right direction?

UPDATE

I updated my code to ensure that the repo is returning null.

Parent parent = parentRepository.Find(command.ParentID);
if (parent == null)
    child.parent = null;
else
    child.parent = parent;

Debugging shows that the line child.parent = null; is being executed, yet when I check child.parent after this line, it still shows a reference to the previously referenced Parent object. But every now and then, it is correctly setting to null. This version works:

Parent parent = parentRepository.Find(command.ParentID);
if (parent == null)
{
    child.parent = null;
    child.parent = null;
}
else
    child.parent = parent;

Why do I have to set it to null twice to get consistant behaviour? Do I have to explicitly declare that Parent is nullable on the Child entity somehow?

  • 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-29T09:21:36+00:00Added an answer on May 29, 2026 at 9:21 am

    Yes, you do need to declare that the parent is nullable.

    If you expose a foreign key property on the child, it has to be nullable. So if your child has a ParentId property, it should look like this:

    public int? ParentId { get; set; }
    public virtual Parent { get; set; }
    

    If you are using the fluent API, you can tell EF that the parent is nullable like this:

    modelBuilder.Entity<Child>.HasOptional(d => d.Parent).WithMany(p => p.Children);
    

    Or like this, if you declare the relationship from the principal end rather than the dependent end:

    modelBuilder.Entity<Parent>.HasMany(p => p.Children).WithOptional(d => d.Parent);
    

    It is strange that the child.Parent needs to be set to null twice. Have you tried stepping into the code to see why? You could also try this for your Parent property, to see exactly what is going on:

    private Parent _parent;
    public virtual Parent
    {
        get { return _parent; }
        set { _parent = value; }
    }
    

    This is code you can actually step into, to make sure that the private field is being set to null.

    Response to comment

    It does sound like there is an issue with lazy loading going on. Try this.

    In your repository Find method, eager load the parent. You can do that with something like this:

    context.Children.Include(c => c.Parent).Find(id);
    

    This would make child.Parent already loaded on the context, and setting to null once should suffice.

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

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from

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.