none
Bast way to Save Object recursive RRS feed

  • Question

  • Hello guys,

     

    I have two softwareparts the first one is a WinForm client the second one is WinService. WinService is using Linq2Sql as DBConnection. The communication between Server and Client is going throw WCF (SOAP) this works fine. Also I get all Objects throw WCF from Linq2SQL. On Client site I modify the objects and send these back to save them. And here is my problem. the object wich I send back to service include one-to-one, one-to-many and many-to-many relationhips. Sometime I got an Exception that the kay is already exist or that he cannot modify an object because "the object is not exist".

    If I use the following code:

          db.Groups.Attach( gr );
          db.Refresh(RefreshMode.KeepCurrentValues, gr);
          
          db.SubmitChanges();
          db.SubmitChanges();
          db.Dispose();
    

    the linq2sql modify only the properties (one-to-many).

    I'm looking for the best way to solve this problem. Beacuse the DataContext should save all data recursive.

     

    Thank

    Eugen

    Friday, July 15, 2011 8:09 AM

All replies

  • Hello Eugen,

    Thanks for your post.

    According to your description, you said that you modified something including one-to-one, one-to-many and many-to-many relationhips and send them back to service together, right? If so, I think you used a global DataContext.

    The reason you sometimes got the exception like you said, is that when you call Attach() method, you used a DataContext which is already loaded. To avoid this, I recommend you can use more than one datacontext, when you modify one place, and then you submitchanges to call back to service. In this way, I think you can use your datacontext easily.

    I hope this can help you.

     

    Have a nice day,


    Jackie Sun [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.

    Monday, July 18, 2011 5:56 AM
    Moderator
  • Hello,

    thanks for your answar. But I always use a new DataContext.

    public void Save(Group gr){<br/>using ( DataContext db = new DataContext( new DatabaseLib.Properties.Settings().connectionString ) )
       {
        var group = from g in _dc.groups where g.id == gr.id select g;
    
        if ( group.Count() < 1 )
        {<br/>     //This works fine!<br/>      db.Groups.InsertOnSubmit( gr );
         db.SubmitChanges();
        }
        else
        {
         try
         {
          
          result = ( from g in db.Groups where g.idGroup == gr.idGroup select g ).First();
          result.ManagedTypeToGroups = gr.ManagedTypeToGroups; //(this list contains new Objects and Modified Objects)
          result.ObjectToGroups = gr.ObjectToGroups; //(same here)
          db.Groups.Attach( gr );
          db.Refresh( RefreshMode.KeepCurrentValues, gr );
    
          db.SubmitChanges();
          db.Dispose();
          _dc = db;
    
         }
         catch ( Exception e1 )
         {
          Console.WriteLine( e1.Message );
         }<br/>   }<br/>}
    


    kind regards

    Eugen

    Wednesday, July 20, 2011 3:14 PM
  • Hi Eugen, I found that you used "db" after 'db.Dispose();', which I think is not exact correct. Because db is an instance of DataContext, if it called Dispose() method, it will become null. And it is not necessary to do " _dc = db;".

    Another, you use Using block, and there's no need to call the Dispose() method. Could you please delete the "db.Dispose();" statement, and to have a check? If you have any feedbacks, please feel free to let me know.

    Hope this can help you.

     

    have a nice weekend,


    Jackie Sun [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.

    Friday, July 22, 2011 5:30 AM
    Moderator