问题描述
我曾经在EF 4.0中使用ObjectSet
,我可以使用
i used to use the ObjectSet
in EF 4.0, I could get the underlying Context for this ObjectSet
using
myObjectSet.Context
会返回ObjectContext
.
现在在Ef4.1中带有DbSet
,等效是什么?
Now with DbSet
in Ef4.1, what is the equivalent ??
推荐答案
EF 4.1不提供公共API从DbSet
实例获取DbContext
.您将必须使用Reflections API来获取DbContext
实例.
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.
修改
一种解决方法是将DbContext
实例与DbSet
实例一起传递.例如,如果您有一个仅接受DbSet
实例的构造函数.还要通过DbContext
实例.
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){}
其他方法是在 Reflector 内打开EntityFramewrk.dll
,然后了解如何获取DbSet的内部/私有字段来实例化c5>.我不建议您使用此功能,因为实现方式可能会发生变化.
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.
这篇关于ObjectSet.Context与DbSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!