How would I insert multiple rows to a lookup table with EF without receiving this error: New transaction is not allowed because there are other threads running in the session
?
I have a PostTags lookup table where I many tags can be from a post, this is what I currently have for my update method, the error seems to come from the foreach
loop where I insert the tags (I'm using unit of work, poco, ef 4 cpt5 repository pattern in this post - Entity Framework 4 CTP 4 / CTP 5 Generic Repository Pattern and Unit Testable):
if (ModelState.IsValid)
{
post.FriendlyUrl = Utils.ToFriendlyUrl(post.PostedDate.ToString("yyyy/MM/dd") + "/" + Utils.RemoveAccents(post.Title));
var tags = post.TagsString.TrimEnd(' ', ',').Split(',').ToList();
var updatePost = Mapper.Map<PostModel, Post>(post);
var postTags = new List<int>();
foreach (var tag in tags)
{
postTags.Add(_tag.CheckExistingTag(tag.Trim()));
}
_post.UpdatePost(updatePost);
_unitOfWork.Commit();
// Remove all existing tags associated with this post
_postTag.RemoveAllTagsInPost(updatePost.Id);
// Insert to the PostTagLookup table the new Tags that associates with this Post
foreach (var tagId in postTags)
{
var newPostTag = new PostTagLookup { PostId = updatePost.Id, TagId = tagId };
_postTag.Add(newPostTag);
_unitOfWork.Commit();
}
}
Thanks.