none
Lazy loading doesn't work for newly created objects RRS feed

  • Question

  • I've got a project that uses ASP.NET MVC, Castle Windsor and the Entity Framework.
    Documentation is sparse and the original developer can't be reached anymore, so it's my job to find out how things work.

    Anyway, an "Order" instance created, and one of the entity properties (e.g. "CUSTOMER") is manually populated using EF calls.
    Other entity properties are not set, only their FK values (e. g. "CountryKey" is set, but not "COUNTRY").
    Lazy loading is enabled.
    The order is then AddObject()ed to the EF context and finally SaveChanges() is called.
    So far, so good.

    But when I try to load the object from the database using context.Orders.First( x => x.ID == id ), an equivalent object is returned, i.e. CUSTOMER is filled with a proxy object, but COUNTRY is still empty.
    ObjectStateManager.GetObjectStateEntry says my object is "Unchanged" and context.Refresh(RefreshMode.StoreWins, order) doesn't help either.

    Now the weirdest thing is that when I load an order with a different ID using the same method, COUNTRY is populated with a proxy object.
    Of course, the CountryKeys are identical.

    What could be the reason for this?
    I suspect that the new instance is kept in cache somewhere, but why doesn't even the Refresh method help then?

    I'm quite new to the EF, so all help is highly appreciated.

    Thanks!


    Hypnose Berlin
    Monday, January 30, 2012 3:08 PM

Answers

  • Hi Alan,

    Thanks, it's good to know I'm not completely crazy. ;-)

    I'll try to create a sample project, but I don't know when I'll get around to it.
    Possibly not before in two weeks or so.

    Thanks,
    Marvin


    Hypnose Berlin
    Thursday, February 2, 2012 11:40 AM

All replies

  • Oh, and with Include("COUNTRY") I always get the correct data, of course.
    But I'd like to understand why that isn't necessary for other ID's than that of the newly created instance...

    Thanks.


    Hypnose Berlin
    Monday, January 30, 2012 4:14 PM
  • Forget my complex description, I could boil it down to this:

    private const string TestCustomerID = "Foo";
    
    private int CreateAndSaveOrder( )
    {
        Order order = new Order( TestCustomerID );
        Debug.Assert( order.CustomerID == TestCustomerID );
        Debug.Assert( order.CUSTOMER == null );
        
        _context.AddObject( _context.Orders.EntitySet.Name, order );
        _context.SaveChanges( );
        
        return order.ID;
    }
    
    private Order LoadOrder( int id )
    {
        return _context.Orders.First(x => x.ID == id);
    }
    
    private void ProblemDemo( )
    {
        // Lazy loading is enabled the whole time
        Debug.Assert( ContextOptions.LazyLoadingEnabled );
    
        int newID = CreateAndSaveOrder( );
        Order newOrder = LoadOrder( newID );
        
        // newOrder.CUSTOMER is always null !!
        Debug.Assert( newOrder.CustomerID == TestCustomerID );
        Debug.Assert( newOrder.CUSTOMER == null );
        
        // Refreshing doesn't help either
        _context.Refresh(RefreshMode.StoreWins, order);
        Debug.Assert( newOrder.CUSTOMER == null );
        
        // Object is "unchanged"
        ObjectStateEntry stateEntry = ObjectStateManager.GetObjectStateEntry( newOrder );
        Debug.Assert( stateEntry.State == EntityState.Unchanged );
        
        int oldID = oldID - 10;
        Order oldOrder = LoadOrder( oldID );
        
        // But with oldOrder, all properties are proxy objects
        Debug.Assert( oldOrder.CustomerID == TestCustomerID );
        Debug.Assert( oldOrder.CUSTOMER != null );
    }
    
    

    Hope this makes my problem a bit clearer. :-)

    Thanks,
    Marvin


    Hypnose Berlin
    Monday, January 30, 2012 9:40 PM
  • Hi Marvin,

    Please refer this link: http://msdn.microsoft.com/en-us/library/bb896272.aspx

    Try to use Eager Loading in your query by Include("").

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 1, 2012 2:14 AM
    Moderator
  • Hi Alan,

    Yes, of course it works with Include("REFERENCE"), but why does it work without it sometimes (well, most of the time actually - where lazy loading is used), but not always (for newly created, but saved, instances)?

    I'd like to understand why the behavior is different.

    Thanks,
    Marvin


    Hypnose Berlin
    Wednesday, February 1, 2012 2:17 AM
  • Hi Marvin,

    Thanks for your feedback.

    It's very strange, it should work if the Lazy Loading is on, would you please demo a small project to repro your scenario and ping me here: v-alache@microsoft.com

    Have a nice day.

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 1, 2012 3:03 AM
    Moderator
  • Hi Alan,

    Thanks, it's good to know I'm not completely crazy. ;-)

    I'll try to create a sample project, but I don't know when I'll get around to it.
    Possibly not before in two weeks or so.

    Thanks,
    Marvin


    Hypnose Berlin
    Thursday, February 2, 2012 11:40 AM
  • Hi Marvin,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 9, 2012 7:11 AM
    Moderator
  • Hi Alan,

    Sorry, I haven't had time to create a demo project yet.
    I'll get to back to you as soon as I can demo the issue, but as things work now, I don't know when I'll have the time to do that.

    Thanks,
    Marvin


    Hypnose Berlin

    Thursday, February 9, 2012 9:23 AM