DbContext is a bridge between the entity classes and databases, DbContext responsible for interacting with data , the main role:
1, DbContext contains all entity to entity sets the database tables (DbSet <TEntity>).
2, DbContext the LINQ-to-Entities query into a SQL query and sends it to the database.
3, change tracking: It tracks changes after each entity to modify the query from the database.
4, persistent data: it is also based on solid state insert, update and delete operations to the database.
The DbContext DbSet
Or represents the set of all sets DbSet specified type of database queries from all entities in the context of a specified type of entity.
DbSet common method
TestDBContext tdb = new TestDBContext();
Add(Entity)/AddRange | Add return The Entity |
Add context to the entity, and the state flag to an entity Added tdb.Persons.Add(entityModel); |
AsNoTracking<Entity> | Obtaining a context is not buffered and tracking sequence, sequences used for read-only var personList = tdb.Persons.AsNoTracking<Person>().ToList<Person>(); |
|
Attach(Entity) | Add return The Entity |
Adding to the context entity context tdb.Persons.Attach(entityModel); |
Find(int) | return the corresponding id The Entity |
Acquiring entity object by primary key, if not in the database context and then in return null, Note: The entity object is returned but not yet written to the database exists in the context of Person personEntity = tdb.Persons.Find(1); |
Include | include foreign keys must be connected to, and executed immediately; the Join button no external connections, to delay execution var personList = tdb.Persons.Include("PersonName").ToList<Person>(); var personList= tdb.Persons.Include(s => s.PersonName).ToList<Person>(); |
|
Remove/RemoveRange | retrun delete The entity |
Delete instance and instance objects are added to the deleted mark tdb.Persons.Remove(EntityModel); |
SqlQuery | Gets an instance of a collection by sql, the default return of the collection is being tracked, you can use AsNoTracking () Unfollow var personEntity = tdb.Persons.SqlQuery("select * from T_Person where id = 1").FirstOrDefault<Person>(); |
EF five states in the entity:
1. detached : the entity is not within the context of the tracking range, just as a new entity, the context can be added to the Attach (), The status is unchanged.
2. Unchanged : not changed, just as the read out of the database entity.
3. added : Add state flag is typically performed Add / AddRange added. Because the new object is not recorded in the database, it can not be turned deleted and modified state.
4. deleted : delete state, is generally execution flag Remove / RemoveRange deleted, added status can not be converted.
5. The Modified : Modified state, changing the attributes of the entity will be in this state, the state can be turned deleted, added status can not be converted.
How to determine the status of EF
(Need to keep track of) to create a DbEntityEntry object for the current object, which contains:
Current Value (current value) / Original Value (raw value) / Database Value (the value in the database)
Need only compare these three values, it is easy to know which properties are modified.
Set context.Configuration.AutoDetectChangesEnabled = false is not to track, the default is true.
You can also specify their own state: