简体   繁体   English

为什么在使用实体框架时看不到本地属性?

[英]Why can I not see the property Local when using Entity Framework?

I know I am missing something completely obvious, but why can't I see the property Local when working with an EF context? 我知道我遗漏了一些显而易见的东西,但是为什么在使用EF上下文时看不到属性Local The typical example online looks like the following: 网上的典型示例如下所示:

context.Parents.Local

But I am unable to see the Local property. 但是我看不到Local属性。 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. 现在,据我了解,Local是DbSet类的成员,并且我认为我没有在应用程序中使用DbSet。 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. 我所知道的是,我已经添加了对EntityFramework的引用, using System.Data.Entity而我所熟悉的其他一切工作都很好。 Except Local isn't there, and I really do need some sorting capability for my DataGridView . 除了Local不存在之外,我确实确实需要对DataGridView排序的功能。

Help! 救命!

DbContext , DbSet etc. are part Entity Framework 4.1/4.2 which is shipped, among others, as the EntityFramework NuGet package. DbContextDbSet等是Entity Framework 4.1 / 4.2的一部分,它作为EntityFramework NuGet软件包提供。 With just the .NET Framework you only have what is now called "Core EF Runtime" according to this : 只需在.NET Framework你只有现在所谓的“核心EF运行”,按照

In short, you need to install EF 4.1/4.2. 简而言之,您需要安装EF 4.1 / 4.2。

If you look at your auto-generated code for the context class, does it inherit from DbContext or ObjectContext? 如果查看上下文类的自动生成的代码,它是否继承自DbContext或ObjectContext?

If it is inheriting from ObjectContext, there are a few more steps you need to take. 如果它是从ObjectContext继承的,则还需要执行几个步骤。

  • On the model design surface, Right Click->Add Code Generation Item 在模型设计图面上,右键单击->添加代码生成项
  • Select 'Code' from the menu on the left 从左侧菜单中选择“代码”
  • Select 'ADO.NET DbContextGenerator' 选择“ ADO.NET DbContextGenerator”
  • Name the item the same as your model (ex: 'MyModel') 将项目命名为与模型相同的名称(例如:“ MyModel”)
  • Click Add 点击添加

Two *.tt files will be added to your project called something like MyModel.tt and MyModel.Context.tt. 两个* .tt文件将被添加到您的项目中,名为MyModel.tt和MyModel.Context.tt。

After a recompile, look at your auto-generated code for the context again and it should now inherit from DbContext. 重新编译后,再次查看该上下文的自动生成的代码,它现在应该继承自DbContext。 Your Parents object will now be of type DbSet instead of ObjectSet and you will now be able to use the Local property. 现在,您的Parents对象将是DbSet类型而不是ObjectSet类型,并且您现在将能够使用Local属性。

暂无
暂无

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

相关问题 在实体框架中使用DbSet <TEntity> .Local属性 - Using DbSet<TEntity>.Local Property in Entity Framework 为什么我不能使用 Entity Framework 提交带有导航属性的表单? - Why can I not submit a form with a navigation property with Entity Framework? 为什么我看不到对象的详细信息(ID,名称等)。 我正在使用MVC 4,剃刀,实体框架 - Why I can't see the details of an object(ID, name,etc). I am using mvc 4, razor, entity framework 使用实体框架时,为什么不能访问外键字段的值? - Why can't I access the value(s) of foreign key field(s) when using Entity Framework? 为什么我可以在Entity Framework数据模型中看到存储的proc和标量函数,而在控制器中却看不到? - Why can I see stored procs and scalar functions in Entity Framework data model but not my controller? 当我从导航属性集合中删除实体时,为什么实体框架没有删除该实体? - Why Entity Framework does't Delete the entity when I remove it from navigation property collection? 使用实体框架时,设置 FK 时应该设置导航属性还是外键属性? - When using Entity Framework, should I set the navigation property or foreign key property, when setting the FK? 当属性验证依赖于另一个属性值时,如何使用元数据验证实体框架 4 Model? - How do I Validate An Entity Framework 4 Model Using Metadata When The Property Validation Is Dependent on Another Property Value? 我如何在实体框架中看到执行命令? - How can i see execute command in Entity FrameWork? 如何查看实体框架的未决更改? - How can I see the Entity Framework's pending changes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM