2011-01-28 14 views
0

このエラーを受け取らずに、EFでルックアップテーブルに複数の行を挿入するにはどうすればいいですか?New transaction is not allowed because there are other threads running in the sessionEntity Framework 4 CTP 5 POCO - ルックアップテーブルに複数の行を挿入しますか?

私は多くのタグが投稿のものであるポストタグルックアップテーブルを持っています。これは私の現在の更新メソッドにあります。エラーはforeachループから来ているようですこの記事での作業、POCOのユニット、EF 4 cpt5リポジトリパターン - 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(); 
    } 
} 

感謝。

答えて

0

あなたのモデルは見たことがありませんが、EFではルックアップテーブルについて心配する必要はありません。EFがマッピングを行います。

は、だから、このような何かを行うことができます。

var post = new post(); 
post.Tags.Add(new Tag()); 
_postRepository.Add(post); 
_unitOfWorkCommit(); 

または

var post = new post(); 
var tags = new List<Tag>{new Tag {Post = post}, new Tag {Post = post}}; 
_postRepository.Add(post); 
_unitOfWorkCommit(); 

または

var post = new post(); 
var tags = GetTagList(); 
foreach(var tag in tags) { 
    post.Tags.Add(tag); 
} 

_postRepository.Add(post); 
_unitOfWorkCommit(); 
+0

こんにちはポール、私はとの問題を抱えた多対多の関係EFので、私はルックアップテーブルに行くことに決めました。ここで私の問題と同様に、多対多に関する私の最初の質問です:http://stackoverflow.com/questions/4571188/entity-framework-4-many-to-many-insertion and http://stackoverflow.com/questions/4589258/entity-framework-4-ctp-5-poco-many-to-many-or-lookup-tableを参照してください。 – Saxman

関連する問題