简体   繁体   中英

How can I read the non public member "_disposed" from EF CORE DbContext

I would like to check if ef core context is disposed.

I tried:

context.GetType().GetMembers(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)

context.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)

context.GetType().GetProperties(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)

Not listing the property.

I do not know why anyone might need it, but you can do it this way:

public class AppContext : DbContext
{
    public override void Dispose()
    {
        IsDisposed = true;
        base.Dispose();
    }

    public bool IsDisposed { get; private set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM