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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:43:54+00:00 2026-06-17T11:43:54+00:00

I am using EF 5 and I have lazy loading enabled. When I retrieve

  • 0

I am using EF 5 and I have lazy loading enabled. When I retrieve an Entity from database it works perfectly.

Here is my problem. I have a generic repository to perform database operation.

    public int Update(T t) //Update method implemented at repository layer
    {
        dbSet.Attach(t);
        context.Entry(t).State = EntityState.Modified;
        return context.SaveChanges();
    }

    public T Update(T t, int id) //This Method calls the above method to
    {
        if (Update(t) > 0)
        {
            //Now entity is updated so retrieve the entity from the database.
            return Get(id); //This line of code doesn't return entity with reference. It does return the updated entity.
        }
        return null;
    }

Now when I query for the Entity using the primary key to get an updated entity it gives me updated entity, however without any reference properties. I can not user Lazy loading here because it throws an exception.

After updating enttity, I noticed dbSet.Local has the updated entity. So I tried to clear to before I retrieve updated Entity but no luck. I also tried reloading Entity via context but it doesn’t reload navigational properties. I cannot use Reference property as I an using generic repository.
The only way I can accomplish is to dispose and create new instance of context and dbset.

I want to return updated entity with relational properties filled. Does anyone have a good solution.

  • 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-17T11:43:55+00:00Added an answer on June 17, 2026 at 11:43 am

    I have lazy loading enabled

    I am attaching POCO entity

    I am assuming from your comments that somewhere in your application you are instantiating your entity like so new MyEntity() and as such lazy loading will not work as it is not a proxy POCO.

    The easiest way to do what you’re trying to do, considering you say you have lazy loading enabled, is to work with the proxy POCOs. That is use the below to instantiate an entity wherever that may be:

    MyEntity entity = MyContext.MyEntities.Create();
    

    Lazy loading should then work for you. If you don’t want to do this or this does not work then the best option is to pull the existing entity (as a dynamic proxy) from the database and populate from your POCO. So in your repository update method:

    Edit

    I should note that it is also possible to do this without a round trip to the db. See comments.

    public T Update(T poco)
    {
      //get the entity from db
      T proxyPoco = context.Set<T>().Find(id);
    
      //alternatively just create the proxy, set the id and attach.
      //no db retrieval.
      //T proxyPoco = context.Set<T>.Create();
      //proxyPoco.Id = poco.Id;
      //context.Set<T>.Attach(proxyPoco);
    
      if(proxyPoco == null) 
      {
        //throw an exception or handle case where the entity is not found.
        //unecessary if using alternative above.
      }
      else 
      {
        //set the proxy poco values using your original poco
        context.Entry<T>(proxyPoco).CurrentValues.SetValues(poco);
      }
      context.SaveChanges();
      return proxyPoco;
    }
    

    Because you return the proxy POCO lazy loading should work.
    Other less desirable options are to:

    1. Discard context and get entity again.
    2. Use reflection to explicitly load reference and collections for that entity.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Table View which displays images using lazy loading from AFNetworking with
NOTE: I am using ARC I have a lazy image loading technique that works
I'm using NHibernate to administer my entities, and to have lazy loading enabled I
i need to retrieve the page title from an URL i already have using
I have a Silverlight 4 app which pulls entities down from a database using
I have successfully implemented lazy-loading of images in my app using the example provided
I have a problem with lazy loading in hibernate when dealing with inheritance. I
I am writing a generic repository to interface with EF using DBContext. I have
In Entity Framework 4, what is the difference between Lazy Loading, and using Load()
I have a project by NHibernate implementation and using Lazy Loading. I have two

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.