5

i used to use the ObjectSet in EF 4.0, I could get the underlying Context for this ObjectSet using

myObjectSet.Context which returns ObjectContext.

Now with DbSet in Ef4.1, what is the equivalent ??

Mustafa Magdy
  • 1,230
  • 5
  • 28
  • 44
  • Possible duplicate of [Can you get the DbContext from a DbSet?](http://stackoverflow.com/questions/17710769/can-you-get-the-dbcontext-from-a-dbset) – Michael Freidgeim Jul 13 '16 at 03:00

1 Answers1

3

EF 4.1 does not offer a public API to get the DbContext from the DbSet instance. You would have to use Reflections API to get the DbContext instance.

Edit

One workaround would be to pass around the DbContext instace with DbSet instance. Eg if you had a constructor that took only a DbSet instance. Pass the DbContext instace also.

public MyService(DbContext context, DbSet<MyClass> mySet){}

Other method would be to open up the EntityFramewrk.dll inside Reflector and find out how to get the DbContext instace by accessing the internal/private fields of DbSet. I wouldn't advice you to use this because implementations can change.

Eranga
  • 32,181
  • 5
  • 97
  • 96