ObjectSet.Context vs DbSet

时光怂恿深爱的人放手 提交于 2019-12-01 05:29:53

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.

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