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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:31:06+00:00 2026-05-31T12:31:06+00:00

Entity: public class Page { //… public virtual Page Parent { get; set; }

  • 0

Entity:

public class Page
{
    //...
    public virtual Page Parent { get; set; }
}

Need to set Parent field to null. Tried this, but no luck:

// Existing entity
Page pageAttached = db.Pages.First(x => x.Id == page.Id);
db.Entry(pageAttached).CurrentValues.SetValues(page);
if (model.ParentId != null)
    pageAttached.Parent = db.Pages.First(x => x.Id == model.ParentId);
else
    pageAttached.Parent = null;    //does nothing
db.SaveChanges();
  • 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-31T12:31:08+00:00Added an answer on May 31, 2026 at 12:31 pm

    Parent is not a “complex field”, it is a “Navigation Property”.

    Does it work if you do this?

    // Existing entity
    Page pageAttached = db.Pages.Include(x => x.Parent).First(x => x.Id == page.Id);
    db.Entry(pageAttached).CurrentValues.SetValues(page);
    if (model.ParentId != null)
        pageAttached.Parent = db.Pages.First(x => x.Id == model.ParentId);
    else
        pageAttached.Parent = null;    //does nothing
    db.SaveChanges();
    

    Response to comment 1

    No, I meant .Include(x => x.Parent). I prefer strongly-typing using the lambda overload. Keeps magic strings out of the code.

    The reason this works is because DbContext uses dynamically generated proxy classes for lazy loading. When you only query for .First(x => x.Id == page.Id), the object returned is really a class that implements your Page entity as its base class. (This is why collection and navigation properties have to be marked virtual, so they can be overridden in the dynamic proxies.) Furthermore, the dynamically generated proxy has a null Parent reference, even if there is a parent in the db.

    It is not until the Parent property get method is invoked that EF hits the db to lazily load the parent. This is when it finds out whether the db actually has a null or non-null Parent property. So, when you set .Parent = null before the parent is actually lazily loaded, EF does nothing because it’s already null.

    The code I suggested uses .Include to eager load the Parent property. This means that the db gets both the child + its parent in a single db call. Now when you set null, the DbContext will track the change and remove the relationship during your next SaveChanges.

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

Sidebar

Related Questions

Imagine a class like this: public class MyEntity : Entity { public virtual States
Suppose I have public class Product: Entity { public IList<Item> Items { get; set;
Looks like stupid question, but I just dont get it. My entity: public class
@Entity public class TestClass implements Serializable{ private Integer id; private Set<String> mySet; @Id @GeneratedValue
Imagine I have an entity: public class MyObject { public string Name { get;
mI've this entity class: @Entity public class User implements Serializable { @Id @GeneratedValue(strategy=GenerationType.AUTO) private
I have a Customer class defined like this: @Entity public class Customer { //...
Let's say there is a entity class like this @Entity public class User {
Using Entity Framework. Simple Page class with tree hierarchy. public class Page { public
I have an entity class like this (with lots of stuff missing): class Parent

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.