none
updating the database using an object i received from the client RRS feed

  • Question

  • I have a datacontext class and a linq.Table object which represents a datatable with relationships to other tables, the client edits the object and sends it back through web service.

    How can i update the object without having to run on each field and each list in the object and assign it the new value?

    using 

    db.Cards.Attach(card,oldCard);
    

    gives me exception: Cannot add an entity with a key that is already in use.

    Thursday, July 28, 2011 1:26 PM

Answers

  • Hi Ronenfe,


    I think the problem is that your datacontext cannot track the same entity more than once. 

    You could try something like this: (you should create a new data context)

       MyDataContext db = new MyDataContext();
      db
    .Attach(card);
      db
    .Refresh(RefreshMode.KeepCurrentValues, card);
      db
    .SubmitChanges(); 

     

    Let me know how it works out for you.

    For more information you could check this page: http://www.west-wind.com/weblog/posts/135659.aspx

    And here you can find an MS site about how should these type issues been handled: http://msdn.microsoft.com/en-us/bb546187%28VS.90%29.aspx

     

    HTH


    Regards, Peter

    //If a post answers your question, please click "Mark As Answer".
    //Also if a post seems to be helpful, please click "Mark as Helpful" on that post.
    • Marked as answer by ronenfe Thursday, July 28, 2011 4:15 PM
    Thursday, July 28, 2011 2:59 PM

All replies

  • thanks, I already have read it, there must be a simple way to do that though.
    Thursday, July 28, 2011 1:51 PM
  • Hi Ronenfe,


    I think the problem is that your datacontext cannot track the same entity more than once. 

    You could try something like this: (you should create a new data context)

       MyDataContext db = new MyDataContext();
      db
    .Attach(card);
      db
    .Refresh(RefreshMode.KeepCurrentValues, card);
      db
    .SubmitChanges(); 

     

    Let me know how it works out for you.

    For more information you could check this page: http://www.west-wind.com/weblog/posts/135659.aspx

    And here you can find an MS site about how should these type issues been handled: http://msdn.microsoft.com/en-us/bb546187%28VS.90%29.aspx

     

    HTH


    Regards, Peter

    //If a post answers your question, please click "Mark As Answer".
    //Also if a post seems to be helpful, please click "Mark as Helpful" on that post.
    • Marked as answer by ronenfe Thursday, July 28, 2011 4:15 PM
    Thursday, July 28, 2011 2:59 PM
  • well, thanks allot, it seems to be in the right direction. now the main object is updated, but what if i want to update also the tables which is in a relationship with that table? i mean, the object contains list which represent rows in other tables in the database that are linked to main table by foreign key to primary key relationships (the main table have foreign keys which are linked to  primary keys in other tables).

    nothing happens to them if i attach the card object which contains the changes in them.

    trying to change update rule specification to cascade in the table configuration didn't help .

    also maybe you also know how to insert a new card object which insert its row to the database and also will insert all rows in the linked tables?

     

    Thursday, July 28, 2011 3:28 PM
  • I think it will be similar to the delete. As LINQ to SQL does not support or recognize cascade-delete operations and you have to delete the linked objects before you can delete the main object.

    Here I think you will have to do the same for the linked objects that you do for the main object to update them.


    Regards, Peter

    //If a post answers your question, please click "Mark As Answer".
    //Also if a post seems to be helpful, please click "Mark as Helpful" on that post.
    Thursday, July 28, 2011 3:41 PM
  • actually using cascade for deleting worked perfectly for me.

    see this link:

    http://blogs.msdn.com/b/bethmassi/archive/2007/10/02/linq-to-sql-and-one-to-many-relationships.aspx

    Thursday, July 28, 2011 4:01 PM
  • Yes, I know this "trick" I needed it when I used lookup tables...

     

    BUT:

    "LINQ to SQL does not support or recognize cascade-delete operations. If you want to delete a row in a table that has constraints against it, you must complete either of the following tasks:

    • Set the ON DELETE CASCADE rule in the foreign-key constraint in the database.

    • Use your own code to first delete the child objects that prevent the parent object from being deleted."

     

    http://msdn.microsoft.com/en-us/library/bb386925.aspx


    Regards, Peter

    //If a post answers your question, please click "Mark As Answer".
    //Also if a post seems to be helpful, please click "Mark as Helpful" on that post.
    Thursday, July 28, 2011 4:06 PM