ObjectSet.Context vs DbSet

我的未来我决定 提交于 2019-12-01 03:34:02

问题


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 ??


回答1:


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.



来源:https://stackoverflow.com/questions/8198023/objectset-context-vs-dbset

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