none
Linq relations update RRS feed

  • Question

  • Hi All!.
    I have next problem with linq 2 sql.
    I have Database with Articles and comments tables. Comment  have articleId field. So article  is one to many relation to comment. Linq2sql generate for me Comments enumeration as article part. So i can see any comment to article in Atticle.Comments,  i can add comment to article with article.Comments.Add etc... But if I adding new comment c=new Comment{.....,ArticleID=someId...} with call  InsertOnSubmit(c), SubmitChanges() I  did not see this comment in article.Comments where article.articleId is "someId" . So Comments enumeration is not updated from database, how I can resolve this problem ?
     
    Regards Alex
    • Changed type AlexDov Sunday, January 10, 2010 6:36 AM
    Saturday, January 9, 2010 10:02 PM

Answers

  • Alex,

    If you retrieve article.Comments before adding the new Comment to the database with SubmitChanges(), then accessing article.Comments again will not update the collection with your new Comment. If, on the other hand, you set the Article property instead of the ArticleID, it should appear in article.Comments.

    Does that help?

    David

    Blog - http://blogs.rev-net.com/ddewinter/ Twitter - @ddewinter
    • Marked as answer by AlexDov Tuesday, January 12, 2010 6:15 AM
    Monday, January 11, 2010 6:01 PM
    Answerer

All replies

  • Alex,

    If you retrieve article.Comments before adding the new Comment to the database with SubmitChanges(), then accessing article.Comments again will not update the collection with your new Comment. If, on the other hand, you set the Article property instead of the ArticleID, it should appear in article.Comments.

    Does that help?

    David

    Blog - http://blogs.rev-net.com/ddewinter/ Twitter - @ddewinter
    • Marked as answer by AlexDov Tuesday, January 12, 2010 6:15 AM
    Monday, January 11, 2010 6:01 PM
    Answerer
  • David, thank you a lot.
    At this moment i have changed approach to this problem, but i am sure that your solution will work fine and will try it soon.
    Regards Alex.

    Tuesday, January 12, 2010 6:16 AM