ObjectSet.Context与DbSet [英] ObjectSet.Context vs DbSet

查看:93
本文介绍了ObjectSet.Context与DbSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆