O'Reilly logo

Programming Entity Framework: DbContext by Rowan Miller, Julia Lerman

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

Chapter 8. Using DbContext in Advanced Scenarios

The focus of this book so far has been to get you up and running with using the DbContext, along with its partner APIs—Validation and Change Tracking. Now it’s time to look at some advanced and less commonly used features of DbContext, the DbSet and the Database classes, as well as moving between a DbContext and ObjectContext. Even though this book is not an application patterns book, we will also take a look at two interesting application scenarios. One will be a discussion of defining your DbContext and taking into consideration the use of multiple contexts in your application to target only the sets of model classes that are needed in any given scenario. The other will be a look at leveraging the IDbSet to create abstractions that will allow you to build more flexible applications. In Programming Entity Framework, 2e, you’ll find an extensive sample that uses ObjectSet, automated unit testing, and repositories. This IDbSet example will be a slice of that, explaining how you can replicate the pattern using the DbContext API.

Moving Between ObjectContext and DbContext

As you’ve learned, the DbContext is a smaller API exposing the most commonly used features of the ObjectContext. In some cases, those features are mirrored in the DbContext API. In other cases, the Entity Framework team has simplified more complex coding by providing us with methods like Find or properties like DbSet.Local. But there’s a big API lurking underneath that ...

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