简体   繁体   English

从DbContext获取ObjectContext

[英]Get ObjectContext from DbContext

I have a DbContext application where I need to use ObjectContext on a certain form (data binding issue with DbContext). 我有一个DbContext应用程序,在这里我需要以某种形式使用ObjectContext(DbContext的数据绑定问题)。 I tried accessing the ObjectContext entities using 我尝试使用访问ObjectContext实体

var oc = ((IObjectContextAdapter)MyEntities).ObjectContext;

but entities do not show up when typing oc.Students . 但是键入oc.Students时实体不显示。 What shall I do to have access to entities? 我应该怎么做才能访问实体?

I believe you're looking for the method CreateObjectSet<TEntity>() 我相信您正在寻找方法CreateObjectSet<TEntity>()

In your scenario, that would be used like so 在您的方案中,将像这样使用

var oc = ((IObjectContextAdapter)MyEntities).ObjectContext;
var query = oc.CreateObjectSet<Student>();

You can then use LINQ to build your query much the same way you would with an IQueryable<Student> 然后,您可以使用LINQ来构建query就像使用IQueryable<Student>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM