Entity Framework - Where is my Object Context?

旧时模样 提交于 2019-12-04 22:54:36

To get to the ObjectContext of your DbContext, all you'd need to do is the following:

var objectContext = ((IObjectContextAdapter)myDbContextObject).ObjectContext;

ObjectContext was replaced by DbContext in Entity Framework 4.1. Actually DbContext is an adapter (wrapper) over ObjectContext. If you need to get ObjectContext you can cast your DbContext instance to IObjectContextAdapter interface (it is implemented explicitly) and wrapped ObjectContext instance will be available:

ObjectContext context = ((IObjectContextAdapter)db).ObjectContext;

BTW I think you are looking for db.Database.SqlQuery method.

In Your case MyEntities is your ObjectContext.

Technically it is auto-generated class by EntityFramework that inherits from ObjectContext class.

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