为什么在使用实体框架时看不到 Local 属性? [英] Why can I not see the property Local when using Entity Framework?

查看:24
本文介绍了为什么在使用实体框架时看不到 Local 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我遗漏了一些非常明显的东西,但是为什么在使用 EF 上下文时我看不到 Local 属性?网上的典型例子如下所示:

I know I am missing something completely obvious, but why can't I see the property Local when working with an EF context? The typical example online looks like the following:

context.Parents.Local

但是我看不到 Local 属性.现在,据我所知,Local 是 DbSet 类的成员,我认为我没有在我的应用程序中使用 DbSet.我所知道的是我已经添加了对 EntityFramework 的引用,有 using System.Data.Entity 和我熟悉的其他一切工作都很好.除了 Local 不存在之外,我确实需要为我的 DataGridView 提供一些排序功能.

But I am unable to see the Local property. Now from what I understand, Local is a member of the DbSet class, and I don't think I'm working with a DbSet in my application. What I do know is I've added a reference to the EntityFramework, have using System.Data.Entity and everything else I'm familiar with doing is working just fine. Except Local isn't there, and I really do need some sorting capability for my DataGridView.

帮助!

推荐答案

如果您查看为上下文类自动生成的代码,它是从 DbContext 还是 ObjectContext 继承的?

If you look at your auto-generated code for the context class, does it inherit from DbContext or ObjectContext?

如果它是从 ObjectContext 继承的,则您还需要执行一些步骤.

If it is inheriting from ObjectContext, there are a few more steps you need to take.

  • 在模型设计图面上,右键单击->添加代码生成项
  • 从左侧菜单中选择代码"
  • 选择ADO.NET DbContextGenerator"
  • 将项目命名为与您的模型相同的名称(例如:'MyModel')
  • 点击添加

两个 *.tt 文件将添加到您的项目中,名为 MyModel.tt 和 MyModel.Context.tt.

Two *.tt files will be added to your project called something like MyModel.tt and MyModel.Context.tt.

重新编译后,再次查看自动生成的上下文代码,它现在应该从 DbContext 继承.您的Parents对象现在将是DbSet而不是ObjectSet类型,您现在可以使用Local属性.

After a recompile, look at your auto-generated code for the context again and it should now inherit from DbContext. Your Parents object will now be of type DbSet instead of ObjectSet and you will now be able to use the Local property.

这篇关于为什么在使用实体框架时看不到 Local 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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