Working with disconnected data RRS feed

  • Question

  • In dealing with disconnected data, I know there are special things you need to do when saving or creating records. For instance:


    Code Snippet

    public class User


    public int Id;


    public bool Save()


    using (MyDataContext dc = new MyDataContext())


    if (this.Id == 0) //-- Create New


    else //-- Save existing

    dc.Users.Attach(this, true);





    In the Attach method, you are telling the DataContext to treat the instance as changed data and update the record in the database. However what happens if you have multiple levels of data in the User class? For instance, what if I want to add a Contact  record to the User.Contacts collection? How do i handled disconnected data in this instance? Do I event need to do anything special at all other than to just add it to the collection and update the User table?


    All help is much appreciated.

    Friday, April 11, 2008 2:43 PM


  • When using Attach, you will need to explicitly attach any entities that you want the DataContext to perform updates on. Associated entities are not automatically attached when you attach the parent.


    Hope this helps.


    Faisal Mohamood

    Sunday, April 13, 2008 5:24 AM