为什么dbcontext在foreach循环之前处理

时间:2013-02-10 18:45:17

标签: linq entity-framework-5 using

我一直在努力弄清楚为什么这个代码在foreach循环之前处理dbcontext。任何线索将不胜感激。谢谢!

    [TestMethod]
    public void CreatePostAddComment()
    {
        IQueryable<Post> thePost;
        using (var _context = new BlogRepository())
        {
            _context.AddPost(GetTestPost());
            var retrivePost = _context.GetPostByName("TestPost1");
            thePost = retrivePost;
        }

        foreach (var post in thePost)
        {
            using (var _dbContext = new BlogRepository())
            {
                _dbContext.AddComment(GetTestComment(post));
            }

        }
    }

0 个答案:

没有答案