none
Get a DbContext from a DbSet RRS feed

  • Question

  • Is there a simpler way to retreive the owner of a DbSet ?

    (object 'o' is a DbSet<T>)

     object internalSet = o.GetType().GetField("_internalSet", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(o);
     object internalContext = internalSet.GetType().GetProperty("InternalContext").GetValue(internalSet, null);
     object newContext = internalContext.GetType().GetProperty("Owner").GetValue(internalContext, null);


    • Edited by fixitchris Wednesday, February 22, 2012 8:31 PM
    Wednesday, February 22, 2012 8:30 PM

Answers

  • Hi,

    This is not simple because apparently you are not supposed to do so as this is a detail implementation. Not sure what is the context (no pun intended) but my personal preference would be likely to avoid doing this. If you need a context, pass it as a parameter ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Friday, February 24, 2012 8:22 PM

All replies

  • Hi fixitchris,

    Welcome!

    I think it works because there are properties storing the Instance information. I think it is hard(or impossible) to find the owner instance according to a property value. Thanks for understanding.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 24, 2012 8:09 AM
    Moderator
  • Hey Alen.

    My code does work but I was hoping for a simpler way to get at the owning context.

    Thanks.

    Friday, February 24, 2012 6:01 PM
  • Hi,

    This is not simple because apparently you are not supposed to do so as this is a detail implementation. Not sure what is the context (no pun intended) but my personal preference would be likely to avoid doing this. If you need a context, pass it as a parameter ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Friday, February 24, 2012 8:22 PM