当使用“ using statement”时,已经处置了dbcontext。 [英] Dbcontext has been disposed when using "using Statement"

查看:67
本文介绍了当使用“ using statement”时,已经处置了dbcontext。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误消息:


由于已处理dbcontext,操作无法完成。

The operation cannot be completed because the dbcontext has been disposed.

有人可以解释为什么我的 DbContext 在执行更新时为何以及在何处处置?

Can somebody explain why and where my DbContext is getting disposed while I perform the update?

上下文文件:

using System.Data.Entity;

namespace OnlineTest
{
   internal class OnlineTestContext : DbContext
   {
       private OnlineTestContext() : base("name=OnlineTest")
       {
       }

       private static OnlineTestContext _instance;

       public static OnlineTestContext GetInstance
       {
           get
           {
                if (_instance == null)
                {
                    _instance = new OnlineTestContext();
                }
                return _instance;
           }
       }

       public DbSet<User> Users { get; set; }
   }
}

业务逻辑:

    public int UpdateUser(User user)
    {
        user.ModifiedOn = DateTime.Now;

        using (var context = OnlineTestContext.GetInstance)
        {
            context.Entry(user).State = EntityState.Modified;
            return context.SaveChanges();
        }
    }

    public User GetUserByEmailId(string emailId)
    {
        using (var context = OnlineTestContext.GetInstance)
        {
            return context.Users.First(u => u.EmailId == emailId);
        }
    }

单元测试:

    [TestMethod]
    public void UpdateUserUnitTest()
    {
        User user = onlineTestBusinessLogic.GetUserByEmailId("test@test");
        user.PhoneNumber = "+91 1234567890";
        int changes = onlineTestBusinessLogic.UpdateUser(user);
        User Modifieduser = onlineTestBusinessLogic.GetUserByEmailId("test@test");
        Assert.AreEqual(Modifieduser.PhoneNumber, "+91 0987654321");
    }

谢谢。

推荐答案

在您第二次调用存储库中的方法时将其丢弃。时间线是这样的:

It is disposed by the second time you call a method on a repository. A timeline is like that:


  1. GetUserByEmailId 被调用, _instance 为空,因此已初始化

  2. GetUserByEmailId 完成,并且处理了上下文。但是对象仍然存在于 _instance 字段

  3. UpdateUser 被称为 _instance 不为null,因此使用
  4. 中返回旧上下文。
  5. context.SaveChanges 被调用,但是由于该上下文对象已经被处理,因此引发了异常

  1. GetUserByEmailId is called, _instance is null, so it is initialized
  2. GetUserByEmailId is completed, and context is disposed. But the object still exists in _instance field
  3. UpdateUser is called, _instance is not null, so the old context is returned in using
  4. context.SaveChanges is called, but since this object of context is already disposed, the exception is thrown

通常,避免这样缓存数据库上下文通常是个好主意。基本经验法则是每个工作单元一个上下文对象。您可以在此线程(由乔恩·斯凯特(Jon Skeet主演!)主演!)中找到有关其原因的更多信息。

This is generally a good idea to avoid caching db context like this. Basic rule of thumb is "one context object per unit of work". You can find some more information about why is it so in this thread (starring Jon Skeet!).

这篇关于当使用“ using statement”时,已经处置了dbcontext。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆