Entity Framework entity status

Starting today we begin to explain the status of the entity and data manipulation EF in this article first explain the state entity.
Our previous study by knowing EF bit up and down the state entity responsible for tracking the position of the entity in the state is the namespace System.Dat.Entity where the EntityState , specifically state the following five kinds:

  1. Detached
  2. Unchanged
  3. Added
  4. Deleted
  5. Modified

Here we come to distinguish explain

零、Detached

Sometimes we just need to show the entity, without the need to update the entity, in order to improve performance, we do not need to EF context of the entity to track, this time we used the Detached state. We only need to use when a query AsNoTracking () query objects out of the afterlife is Detached state. Example code:

var person = db.Person.AsNoTracking().Where(p=>p.Id==1).FirstOrDefault();

Note: Because AsNoTracking method DbQuery class, so to be called first AsNoTracking method.

一、Unchanged

Entity context is tracked in this state, but the value in the database without any changes. If the entity does not exist in the database, but the context of the entity to be tracked, while the entity value has not changed, this time by Attach additional track, then the status flag of the entity an Unchanged . Example code:

using (var db = new EFDbContext())
{
  var user =new User()
  {
    Name = "张三",
    Age = 12
  }
  db.User.Attach(user);
  db.Entry(user).State = EntityState.Unchanged;
  db.SaveChanges();
}

二、Added

When the new operation will be used Added state. Labeled Added state, it indicates the bodies context is tracked in the database but does not exist, when we call SaveChanges when the method of data will be saved into the database. If you want to that entity state flag state, two methods may be used:

  1. Indirectly labeled by Add calling the method, the following sample code:
using (var db = new EFDbContext())
{
  var user = new User()
  {
    Name = "张三"
    Age = 12
  }
  db.User.Add(user);
  db.SaveChanges();
}
  1. An explicit flag by calling Entry method, the following sample code:
using (var db = new EFDbContext())
{
  var user = new User()
  {
    Name = "张三"
    Age = 12
  }
  db.Entry(user).State = EntityState.Added;
  db.SaveChanges();
}

Three, Deleted

If you need to delete the entity from the database, you can use the Deleted state when calling SaveChanges data will be deleted from the database when the method. And Added state, deleting an entity can use two methods:

  1. By calling Remove or RemoveRange method, the following sample code:
using (var db = new EFDbContext())
{
  var user = db.User..Where(p => p.Id == 1).FirstOrDefault();
  db.User.Remove(user);
  db.SaveChanges();
}

Note: the Remove to delete a single entity, RemoveRange used to delete multiple entities.

  1. An explicit flag by calling Entry method, the following sample code:
using (var db = new EFDbContext())
{
  var user = db.User..Where(p => p.Id == 1).FirstOrDefault();
  db.Entry(user).State = EntityState.Deleted;
  db.SaveChanges();
}

Four, Modified

When we modify the data, you need to use the Modified state, when calling SaveChanges data will modify data in the database when the method. EF modify data in only one way, by calling the Entry method, the following sample code:

using (var db = new EFDbContext())
{
  var user = db.User..Where(p => p.Id == 1).FirstOrDefault();
  db.Entry(user).State = EntityState.Modified;
  db.SaveChanges();
}

V. Special Note

Almost all states can pass between .State Entry (T) for the forced state transition manner, since the changes are dependent on the state of Id (except Added). We take a look at the following figure above conversion of five states:
elz3dg.png

Guess you like

Origin www.cnblogs.com/gangzhucoll/p/11260887.html