none
Weird behavior with entity objects and their relations to the ObjectContext object RRS feed

  • Question

  • Hi,

    I am new to LINQ, so I hope that some knows how to solve my problem which is:

    I call a webservice to retrieve some adresses and return the reply as a list of entity objects that could (potentially) end up in my database.
    Before they do, I would like to give the user the opportunity to sort out those that should not be kept. My problem is that when the user hits "save",
    all objects are saved to the database including those that were sorted out.

    After eyeballing the code and making some experiments, I found that all objects that I create using the no-parameter constructor end up in the
    database even though I didnt manually attach them to the ObjectContext (at least I dont see where that should happen).

    If I create a new object (like Adress adr = new Adress()) and only assign those properties that do not represent foreign keys in the database, nothing is saved after the call to SaveChanges. However, if I query the database to get hold of a City object (holding the zipcode and city as strings) and assign it to the City property of my Adress object - that adress object will be saved after SaveChanges.

    I dont understand why the authors of the LINQ framework decided to let new objects attach themselves to ObjectContext objects in this seemingly strange way. One obvious solution would be to simply leave the properties that have reference types empty until I know exactly which objects I would like to save in the database, but what if suddenly you need to query the object at some place in the program, and you need to use the reference property before you are able to determine whether or not the object at hand is a "save" or a "throw away".

    So in short - I can think of a number of workarounds, but it strikes me that I dont know exactly how the framework is supposed to be used. What do you do if you need to create temporary objects (like for display in a table), but you only want to store a subset of them?

    Hope somebody can help. Thaks in advance! 

    Wednesday, July 20, 2011 10:54 PM

Answers

All replies

  • Hello,

    Thank you for posting.

    First of all, could you please provide more detailed code snippets to us to analyze your question?

    In addition, we suggest you can check the SQL trace for monitoring the instance of database engine. Please check this document for more information. http://msdn.microsoft.com/en-us/library/ms181091.aspx

    Please feel free to let us know how it goes.

    Best Regards,


    Larcolais Gong[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.

    Sunday, July 24, 2011 2:53 PM
  • Hi, thanks for taking the time to read my post.

    This code will attach the address object to my ObjectContext. I am looking for a way to get around this. Calling Detach() on the context will work, but then I dont know how to attach it again.

     

                DatabaseEntities entities = new DatabaseEntities();

                var query = from city in entities.City where city.Zip == "XXXX" select x;

     

                Address addr = new Address();

                adr.Road = "XXXX";

                adr.No = "XX";

                adr.City = query.First();

     

                entities.SaveChanges(); 

    Monday, July 25, 2011 8:26 PM
  • Hello again,

    I didn't see anything about Attach object to ObjectContext in your demo code. Generally speaking, AddObject and Attach two methods can attach the object to an ObjectContext. Please check this document for more information. http://msdn.microsoft.com/en-us/library/bb896271.aspx

    Best Regards,


    Larcolais Gong[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, July 27, 2011 5:07 AM