none
Can not delete a category in linq to sql Visiual Studio 2008.... RRS feed

  • Question

  • hi developers,

    I use linq to sql in my application to do some CRUD stuff, however, when I tried to delete an object in my service, VS told me: System.InvalidOperationException: Can not delete entities.

    my code snippet is:

      public static void DeleteCategory(Categories category)
      {
        if (null == category)
          throw new ArgumentException("pass an invalid argument.");
        using (MiniNWModelDataContext db = new MiniNWModelDataContext())
        {
          db.Categories.DeleteOnSubmit(category);
          db.SubmitChanges();
        }
      }
    

    Thanks in advance!

    Tuesday, December 28, 2010 8:00 AM

Answers

  • Hello deviceDev,

     

    Nice to see you again. And thanks for posting here.

    Based on your problem,  how about trying to read this article.

    http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/c4cf89b6-59a3-413d-abbb-83f7c78ab357/

    Maybe you can get some good suggestions by reading it.

    I hope this will help resolve your problem. If anything is unclear, please free feel to let us know.

     

    Thank you for your understanding and support.

    I look forward to hearing from you.

     

    Best Regards,


    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.

    Wednesday, December 29, 2010 5:16 AM
    Moderator
  • Just to add some understanding to Jackie's reply ...

    You get the error because your parameter 'category' is not part of the context 'db', so db doesn't know it and therefore cannot mark it as to be deleted on submit.

    Normally in L2S you have to select the object into the context just to delete it (yes, that's two round-trips to the database and not very efficient). The thread that Jackie has linked you to shows an alternative way whereby you can attach a dummy object rather than selecting it. I haven't done this before so can't warrant the technique, but clearly others have succeeded.

    Wednesday, December 29, 2010 1:31 PM

All replies

  • Hello deviceDev,

     

    Nice to see you again. And thanks for posting here.

    Based on your problem,  how about trying to read this article.

    http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/c4cf89b6-59a3-413d-abbb-83f7c78ab357/

    Maybe you can get some good suggestions by reading it.

    I hope this will help resolve your problem. If anything is unclear, please free feel to let us know.

     

    Thank you for your understanding and support.

    I look forward to hearing from you.

     

    Best Regards,


    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.

    Wednesday, December 29, 2010 5:16 AM
    Moderator
  • Just to add some understanding to Jackie's reply ...

    You get the error because your parameter 'category' is not part of the context 'db', so db doesn't know it and therefore cannot mark it as to be deleted on submit.

    Normally in L2S you have to select the object into the context just to delete it (yes, that's two round-trips to the database and not very efficient). The thread that Jackie has linked you to shows an alternative way whereby you can attach a dummy object rather than selecting it. I haven't done this before so can't warrant the technique, but clearly others have succeeded.

    Wednesday, December 29, 2010 1:31 PM
  • Hello deviceDev,

     

    I am writing here to confirm the status of your thread.  If you have any more concerns, please feel free to let us know.

     

    Thank you very much.

     

    Happy new year!


    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, January 3, 2011 2:14 AM
    Moderator