Eager and Lazy loading RRS feed

  • Question

  • Hi all,

    I have table A witch have relation to many tables , say B,C,D

    in the EF mapping I get Object A with Collection of types B,C,D incide.

    When I'm trying to insert a new object A from the client side , I should supply objects B,C,D before and use the SetLink command.


    My problem , I have to fetch from the server objects B,C,D due to some creteria and the save A.

    But this seems very expensive each time to make many travels to the server till I can save object A.

    I did my code like this :

    A.B = client.B.Where(id=>id.b_id == 1).First(); the same thing to C and D



    Any suggestion to save this roundtrips to the server.


    Tuesday, January 27, 2009 7:45 AM

All replies

  • No no we dont telll you the answer, you have to pay for this kind of support
    --------------joking aside

    Hello Wasimf,

    Basically it depends whether you know the the object already or you are creating them with the new object too.

    But if this helps lets look at the example below:

    If you know the Keys or Ids of the relations then you can use them with out fetching them

    customer with address and gender.

    Customer c = Customer.Create(id = 0, name = "Quentin Tarantino"); 
    c.Address = Address.Create(id = 123, address = null); 
    c.Gender = Gender.Create(id = 1, Name = null); 

    context.AddObject("Customers", c);
    context.AddObject("Addresses", c.Address); 
    context.AddObject("Genders", c.Gender); 
    context.SetLink(c, "Address", c.Address); 
    context.SetLink(c, "Gender", c.Gender); 

    Now if the relations dont have relations themselves you dont have to worry about things I dont quite remember if it could cause any problems. but by knowing the id you can just create the objects and add them to the context so they can be tracked enabling you to create links between them and other objects. on the code above if you are going to reuse the objects because they link to other entities then make sure you keep a reference outside of a looping frame.

    Another way is the create a ChangeInterceptor where if a object is added to the table then you can do all this criteria stuff on the server side.

    Some good methods to get familier with is the TryGetUri and the TryGetEntity


    Hope this helps
    Dont forget to
    mark as an answer

    Daniel Portella
    Blogger from hell

    This posting is provided "AS IS" with no warranties, and confers no rights.

    • Proposed as answer by Daniel Portella Wednesday, January 28, 2009 8:38 PM
    Tuesday, January 27, 2009 12:30 PM