DbContext has been disposed

不想你离开。 提交于 2019-12-05 04:34:23
ken2k

Your context has been disposed somewhere else (not in the code you've shown), so basically when you access it from your Register action, it throws the exception.

Actually, you shouldn't use a static singleton to access to your context. Do instantiate a new DbContext instance for each request. See c# working with Entity Framework in a multi threaded server

In my case, my GetAll method was not calling ToList() method after where clause in lambda expression. After using ToList() my problem was solved.

Where(x => x.IsActive).ToList();

You are probably 'lazy-loading' a navigation property of User in your registration view. Make sure you include it by using the Include method on your DbSet before sending it to the view:

_db.Users.Include(u => u.PropertyToInclude);

Also, sharing DbContexts with a static property may have unexpected side effects.

I used to have the same problem. I solved it doing as it was said above. Instantiate a new instance of your context.

Try using this:

            using (HotelContextProductStoreDB = new ProductStoreEntities())
            {
                //your code
            }

This way it'll be created a new instance everytime you use your code and your context will not be disposed.

Why override the Dispose(bool)?

public partial class HotelContext : DbContext
{
    public bool IsDisposed { get; set; }
    protected override void Dispose(bool disposing)
    {
        IsDisposed = true;
        base.Dispose(disposing);
    }
}

And, then check IsDisposed

public static class ContextManager
{
    public static HotelContext Current
    {
        get
        {
            var key = "Hotel_" + HttpContext.Current.GetHashCode().ToString("x")
                      + Thread.CurrentContext.ContextID.ToString();
            var context = HttpContext.Current.Items[key] as HotelContext;
            if (context == null || context.IsDisposed)
            {
                context = new HotelContext();
                HttpContext.Current.Items[key] = context;
            }
            return context;
        }
    }
}

Maybe, can be an option.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!