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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:07:35+00:00 2026-05-27T03:07:35+00:00

I have defined some models like this (Entity Framework Code-First): public class A {

  • 0

I have defined some models like this (Entity Framework Code-First):

public class A
{
  public int Id { get; set; }
  public int Name { get; set; }
}

public class B
{
  public int Id { get; set; }
  public int Name { get; set; }
  public virtual A ObjectA { get; set; }
}

// model update sample code
public void UpdateModel(int id, string name)
{
  B objB = GetObjBByIdUsingLINQ(id); // this function gets the object using LINQ
  if (objB != null) // <-- if you do a breakpoint here and inspect objB, objB.A != null
  {
    objB.Name = name;
    dbContext.Entry(objB).State = EntityState.Modified;
    dbContext.SaveChanges(); // <-- DbEntityValidationException here because objB.A == null
  }
}

When I Load a model B from the database, and I only change the Name and I update it I get the following error: The ObjectA field is required.

I think this is because ObjectA is lazy loaded. However, when I add a breakpoint after I loaded B from the database, and then I view the contents of B in the variable explorer, A will be loaded, and updating B doesn’t give an error.

Any ideas how to solve this problem?

  • 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-27T03:07:35+00:00Added an answer on May 27, 2026 at 3:07 am

    What is happening is that when you stop in a breakpoint and inspect the value of the property ObjectA inside your objB, you are explicitly loading the property.

    In the GetObjBByIdUsingLINQ(id) method you should use an Include to load your property, for example:

    var objB = from b in dbContext.Bs.Include("ObjectA")
               where b.Id == id
               select b;
    

    Or you can load the property explicitly, instead:

    dbContext.Entry(objB).Reference("ObjectA").Load();
    

    You should note that the first option will hit the database only once. In the second option, you will hit the database twice. That should be taken into account depending on your specific case.
    You can read all about working with related entities in this blog post:
    Using DbContext in EF 4.1 Part 6: Loading Related Entities.

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

Sidebar

Related Questions

I'm working on some Django-code that has a model like this: class Status(models.Model): code
I have some logic, which defines and uses some user-defined types, like these: class
I have a Django Form class defined likes this in Models : class AccountDetailsForm(forms.Form):
I have a model that looks something like this: public class SampleModel { public
I would like to export some macros that I have defined and be able
I have a map defined like this std::map<some_key_type, std::string::iterator> mIteratorMap; And a huge string
I have seen some C++ classes with a destructor defined as follows: class someClass
Let's say I've defined this model: class Identifier(models.Model): user = models.ForeignKey(User) key = models.CharField(max_length=64)
I have an assembly generated using POCO template using Entity Framework (e.g. Company.Models.dll).Besides generated
I'm currently learning Django and some of my models have custom methods to get

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.