O'Reilly logo

Fluent Entity Framework by Rebecca M. Riordan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Finding entities

Querying the entity set directly always queries the database. Querying Local (or the ObjectContext equivalent) always queries the entities in memory. But what if you want to do both? If an entity has been loaded, you want to work with the version in memory. If it hasn’t been loaded, you want to load it. Seems perfectly reasonable to me. Again, both the DbContext and ObjectContext APIs provide a means to do this, and the DbContext version is a bit easier to use. But this time the difference isn’t quite so striking.

With the the DbSet<T>.Find() method, the only (possibly) tricky bit is knowing the primary key of the entity you want.

The DbSet<T>.Find() method will only make a trip to the database if it can’t find the primary key ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required