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 ...

Get Fluent Entity Framework now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.