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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:55:34+00:00 2026-05-29T13:55:34+00:00

I have a problem where a property of a newly added entity is not

  • 0

I have a problem where a property of a newly added entity is not lazy-loaded, if required immediately after adding the entity.

For example:

I have a User entity with a virtual JobRole property:

public class User
{
    public int Id { get; set; }

    public virtual JobRole JobRole { get; set; }
    public int JobRoleId { get; set; }

    public string Name { get; set; }
}

I then add a new User:

public User Add(User user)
{
    var addedUser = _myContext.Users.Add(user);
    myContext.SaveChanges();
    return addedUser;
}

The returned reference to the new User is then passed to a Razor view, where it tries to display the JobRole (eg JobRole.Name). At the point that the User is passed to the View, it has:

  • JobRoleId set correctly to an integer value.
  • JobRole = null

I would then expect JobRole to be lazy-loaded, when used by the View, but it isn’t and results in a null-reference exception.

Is this expected behaviour, or is there a way to get newly added entities to lazy-load their properties?

Thanks in advance for any ideas.

Environment:
Using Entity Framework 4.2 code-first.
Lazy-loading enabled.

  • 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-29T13:55:36+00:00Added an answer on May 29, 2026 at 1:55 pm

    That’s because the DbSet.Add method returns the same object that has been passed to it as argument, which in your case isn’t a proxy instance hence lazy loading is not available.

    You should create the User object that’s being passed to DbSet.Add using the DbSet.Create method, which will return a proxy instance, and then assign its properties before persisting it through the DbContext.

    Here’s an example:

    public User Add(User user)
    {
        var newUser = _myContext.Users.Create();
    
        // Copy the property values from 'user' to 'newUser' e.g.
        // newUser.Name = user.Name
    
        _myContext.Users.Add(newUser);
        myContext.SaveChanges();
    
        return newUser;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with CSS3 property for using my own fonts @font-face. In
I have a problem with calling a property() inside a dynamic model in Django.
I have a problem where in some cases (appears to be where property type
I have a problem with a UIView. This is the code: @property ( unsafe_unretained,
I have a problem. It looks like ARC synchronizes my property with child class.
I have a bizarre problem with a 'double' property in Core Data. I have
I have essentially the same problem discussed here: http://khason.net/blog/dependency-property-getters-and-setters-in-multithreaded-environment/ public static readonly DependencyProperty MyPropertyProperty
I have web-service which give list of property at a particular area My problem
I have problem adding markers into marker Clusterer of Google Maps. Firebug returns error:
I have problem with this exception: Hibernate.HibernateException : Could not create the driver 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.