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

后端 未结 2 1748
渐次进展
渐次进展 2020-12-18 07:29

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

相关标签:
2条回答
  • 2020-12-18 07:57

    DbContext, DbSet etc. are part Entity Framework 4.1/4.2 which is shipped, among others, as the EntityFramework NuGet package. With just the .NET Framework you only have what is now called "Core EF Runtime" according to this:

    In short, you need to install EF 4.1/4.2.

    0 讨论(0)
  • 2020-12-18 08:01

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

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

    • On the model design surface, Right Click->Add Code Generation Item
    • Select 'Code' from the menu on the left
    • Select 'ADO.NET DbContextGenerator'
    • Name the item the same as your model (ex: 'MyModel')
    • Click Add

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

    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.

    0 讨论(0)
提交回复
热议问题