When I wrote this blog, I heard that EF7 were released for half a year, and I was deeply dissatisfied with the role of my old Chaser, who was very hard to spit on.
To get to the point, mention DbContext will have to mention ObjectContext, since the first release of EF, the most important component is ObjectContext, ObjectContext provides a conceptual model for interacting with the database (writing query expressions, executing queries, tracking model modification State, persisting modifications to the database), and interacting with other important EF classes:
- ObjectSet: A collection of entities in memory.
- ObjectQuery: A typed query against a conceptual model.
All of these classes are full of features and functionality-some are complex, and most are only necessary in special cases. After going through the two versions of the Entity Framework (. NET 3.5 SP1. NET 4) it became clear that most developers would only use part of the functionality, but unfortunately for most of the task codes and queries that we often do, it is a bit difficult.
Aware of this, the EF team began to have love for developers, and their solution was to create new classes that encapsulate a subset of the ObjectContext functionality, and developers generally do not have to deal with ObjectContext (with the exception of some advanced topics), These new classes are released along with EF4.1 and are part of the EF4.1.
These new classes refer to: Dbcontext,dbset,dbquery. They are also part of the DbContext API. This new API not only contains DbContext, but also contains some of the new features that the EF team has developed for it. The DbContext API is included in the entity Framework.dll, which also contains the logic to drive code first development, a class library with the. NET is separate and is released in the EntityFramework NuGet package. The main part of the Entity framework is part of the. NET framework (primarily System.Data.Entity.dll). These components that are included in the. NET framework are considered to be "core components" of the Entity Framework, and the DbContext API is completely separate from these "core components". The Entity Framework team is aware that integration within the. NET Framework is not a good choice, and they are working to move more "core components" from the. NET Framework to Entity Framework.dll, new features that release the new Entity Framework do not have to wait until a new version of the. NET Framework is released.
The following table is an overview of the capabilities of the DbContext API
|
DbContext
API Classes/Features |
Related to EF4
Class/function |
Objective |
DbContext API Benefits |
| DbContext |
ObjectContext |
Represents a session connected to a database, providing functions such as querying, state tracking, saving, etc. |
Simplifies most of the ObjectContext functions |
| DbSet |
ObjectSet |
Provides collection operations for entity types, such as Add, Attach, Remove.
Inherits the Dbquery, so it can provide query function |
Simplifies most of the objectset functions |
| Dbquery |
ObjectQuery |
Provides query functionality |
Dbquery's query functionality is integrated in the Dbset,
So do not operate the Dbquery directly |
|
Change
Tracker API |
ObjectContext.
Objectstatemanager |
Get entity change tracking information and related actions (original values, current values)
Subject to context management |
Simpler, more intuitive API |
| Validation API |
N/A |
provides automatic data validation. This API leverages the native validation capabilities of. NET 4 |
The new |
| Code First Model Building |
N/A |
Create a database and its metadata based on existing classes and configuration information |
The new |
How to use the DbContext API in your project
Download the Entity Framework components from the hard nuget and the library package Manager, and then leave everything to the first God IDE of the universe.
Some highlights of the DbContext API
Let's take a look at how the DbContext API can reduce the workload, and how to reduce the amount of ObjectContext to compare.
Example contains person Destination trip three entities
Example 1-1 ObjectContext notation
1 Public classBreakawaycontext:objectcontext2 {3 PrivateObjectset<person>_ people;4 PrivateObjectset<destination>_destinations;5 PrivateObjectset<trip>_trips;6 PublicObjectset<person>people7 {8 Get{return_people?? (_people = createobjectset<person> ("people")); }9 }Ten Publicobjectset< Destination >Contacts One { A Get{return_ Destinations?? (_destinations = -createobjectset< Destination > ("Destinations")); } - } the PublicObjectset<trip>Trips - { - Get{return_ Trips?? (_trips = createobjectset<trip> ("Trips")); } - } +}
Example 1-2 DbContext notation
1 Public classBreakawaycontext:dbcontext2 {3 Publicdbset<person> people {Get;Set; }4 PublicDbset<destination> Destinations {Get;Set; }5 PublicDbset<trip> Trips {Get;Set; }6}
Simplified and optimized at a glance.
reduced and simplified collection operations
in Entity Framework 4, add an instance to the set of entities, You can use Objectcontext.addobject or objectset.addobject. When adding an instance to ObjectContext, ObjectContext needs to know which entity collection this instance belongs to. So for objectset.addobject you must specify the entity set name in the form of text, such as:
Context. AddObject ("Trips", Newtrip);
Use objectset.addobject words slightly simplified point: context.Trips.AddObject (Newtrip); The reason that objectcontext.addobject continues to exist in EF4 is backwards compatibility with earlier versions, but developers are not aware of this and will only be confused by these two types of writing.
Because the DbContext API is new, we don't have to worry about backwards compatibility with earlier versions, so DbContext doesn't provide a way to add an instance to the context, but adds the Add method to the Dbset:
Context. Trips.add (Newtrip);
ObjectContext also provides Attachobject and DeleteObject methods, and the same dbcontext does not provide a similar approach, all of which are available on Dbset: Attach, Remove.
retrieving an entity using ID and Dbset.find
One of the things developers often do is to query the corresponding entity based on key, and in the EF4 era we write the following query statements:
Context. People.singleordefault (P=>p.id=id);
Now Dbset provides the Find method:
Context. People.find (ID);
Using find has an advantage, the call Singleordefault method will always query the database, and the use of the Find method, will first query the memory of the existence of relevant instances, if there is, directly return, if not to query the database, the performance is greatly improved .
-------to be Continue.
DbContext API Introduction