Skip to main content

Using the DbContext API

A 12-part series that goes deep into EF 4.1’s new API
Introduction and Model

Version 4.1 of the Entity Framework contains both the Code First approach and the new DbContext API. This API provides a more productive surface for working with the Entity Framework and can be used with the Code First, Database First, and Model First approaches. This is the first post of a twelve part series containing collections of patterns and code fragments showing how features of the new API can be used.

Connections and Models

Looks at how to use the various constructors on DbContext to configure connection strings and the Code First, Model First, or Database First mode of the context. Also looks at the different ways that sets can be defined on a derived context.

Finding Entities

Demonstrates entity states and shows how to add and attach entities and change the state of an existing entity.

Add/Attach and Entity States

Examines entity states and shows how to add and attach entities and change the state of an existing entity.

Working with Property Values

Shows different ways to get information about property values and manipulate the current, original, and database property values of entities and complex objects.

Loading Related Entities

Demonstrates how to load entities eagerly as part of a query, and lazily either automatically or with an explicit call to the Load method. Also looks at how to use the Query method to make adjustments to the loading query.

Local Data

Looks at how the Local property of a DbSet can be used to look at the local collection of entities in that set and how it this local collection stays in sync with the context. Also demonstrates how Local can be used for data binding. Finally, shows the Entities methods which provide detailed information about tracked entities.

Working with Proxies

How to switch off proxy creation, how to explicitly create a proxy instance, and how to get the actual entity type from a proxy type.

Optimistic Concurrency Patterns

Looks at various patterns for resolving optimistic concurrency exceptions, including database wins, client wins, and a hybrid approach in which the user chooses the resolved values.

Raw SQL Queries

Examines ways in which entities and other types can be queried from the database using raw SQL, and how raw non-query commands can be executed on the database.

Load and AsNoTracking

Looks at the Load extension method for loading entities into the context and the AsNoTracking extension method for returning entities without tracking them.

Automatically Detecting Changes

Looks at when DbContext automatically detects changes in your tracked entities and gives some guidance as to when this automatic change detection should be turned off.

Make Entity Framework Better

Feedback Prioritize the Entity Framework feature suggestions you need to be successful.

More Resources

Entity Framework Team BlogEntity Framework Team Blog

Read new content from the team every week!

Entity Framework Design Blog

Preview and provide feedback on the design of new features

Hands-on Labs

Quickly evaluate or learn about Entity Framework with these hands-on labs.

Channel9 Interview – EF

Go behind the scenes and meet the team!

Entity Framework Developer Forum

Connect with the EF community

ConnectConnect

Submit bugs and provide feedback on Entity Framework

MSDN MagazineMSDN Magazine

Read in-depth articles on Entity Framework

MSDN Docs

Explore detailed documentation on Entity Framework

Entity Framework FAQ

Get your EF questions answered