Linq to sql, insert many to many relationship?

You don't need to SubmitChanges three times. If you have the proper foreign keys in place, you can simply add the references to each other in code then SubmitChanges once. LINQ is awesome like that Post post = new Post() { PostName = entity.

PostName }; context.Posts. InsertOnSubmit(post); Tag tag = new Tag() { TagName = entity. Tags }; context.Tags.

InsertOnSubmit(tag); PostTag pt = new PostTag() { Post = post, Tag = tag }; context.PostTags. InsertOnSubmit(pt); context.SubmitChanges().

You don't need to SubmitChanges three times. If you have the proper foreign keys in place, you can simply add the references to each other in code then SubmitChanges once. LINQ is awesome like that.

Post post = new Post() { PostName = entity. PostName }; context.Posts. InsertOnSubmit(post); Tag tag = new Tag() { TagName = entity.

Tags }; context.Tags. InsertOnSubmit(tag); PostTag pt = new PostTag() { Post = post, Tag = tag }; context.PostTags. InsertOnSubmit(pt); context.SubmitChanges().

That is what I thought, I should not need it three times. I do have proper FK in place. I have 2 FKs.

The problem is if I don't do SubmitChanges, I won't get new PostID and TagID back. How do I insert "PostTag" table? Can you show me some codes how to do it properly.

– feelexit Aug 2 at 20:25 Correct, it is a bit counter intuitive, but LINQ will actually map the new PostID and TagID automatically on the one SubmitChanges call. I have posted a code sample, but it's in an edit and needs to be approved before you can see it. – hspain Aug 2 at 20:27 Thank you, Hspain.

Cannot wait to see your code. I removed first two "SubmitChanges", it failed immediately, because "TagID" and "PostID" didn't get generated. The best I can do is to remove the first SubmitChanges from my code.

– feelexit Aug 2 at 20:50.

You can also call InsertOnSubmit only on one of these objects. Compare with Insert into many-to-many relationship tables.

I made a few attempts and even of I use InsertOnSubmit only on client, entries are inserted also to City and ClientCity. How should I do it correctly?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions