Prologue In previous chapters we have seen how to insert data through EF, with and without sequences. In this one, we are going to see how to Update and delete Data from the DB. Updating data The update of the Entity Data (properties) is a very common and easy action. Before of change any of the properties of the Entity, we can check the EntityState property, and we can see that is EntityState.Unchanged. For making an update it is needed to get the Entity which will be modified. In the following example, I use the GetEmployeeByNumber to get a valid Entity: 1: EMPLEADOS emp=GetEmployeeByNumber(2);
2: emp.Name="a";
3: emp.Phone="2";
4: emp.Mail="aa";
After modifying the desired properties of the Entity, we are going to check again Entitystate property, which now has the EntityState.Modified value.
To persist the changes to the DB is necessary to invoke the SaveChanges function of our context.
1: context.SaveChanges();
After modifying the desired properties of the Entity, we are going to check again Entitystate property, which now has the EntityState.Modified value.
To persist the changes to the DB is necessary to invoke the SaveChanges function of our context.
If we check again the EntityState property we will see that the value will be EntityState.Unchanged.
Deleting Data
Another easy action is to delete an Entity.
The first step to delete an Entity from the DB is to select the entity:
1: CLIENTS selectedClient = GetClientByNumber(15);
2: context.CLIENTES.DeleteObject(clienteSeleccionado);
Before invoking the DeleteObject function, we will check EntityStet which value must be EntityState.Unchanged. After deleting the object, the state will be changed to EntitySate.Deleted.
To commit the action we have to invoke the SaveChanges function. Aftar that, the EntityState property will be EntityState.Detached.
Cascade
Entity Framework lets cascade updates and deletes, although I never see cascade updates.
What is a cascade delete?
A cascade delete is an action that allows to delete all the related object to the object we desire to delete.
This option could be established in the DB manager, or it could be in the EF model designer.
For example:
With a given relation (1-N) between clients and requests.
The common situation must be to let delete those clients whose have no requests.
If we select the relation between both entities, and press the second mouse button, we can see the properties panel of the relation. The props are:
This grid shows the relations indicating the Master table(Clients) and the end point (Cabecera or Requests)
The property “End 1 OnDelete” indicates the action to do when a Entity from the Master will be deleted. There are two options:
- None: No action will be done, it is said, if a Entity has details entities it could not be deleted.
- Cascade: It will delete all related entities to the master Entity.
If we enable the cascade delete in a relation, and we invoke the DeleteObject function of the set, we could observe that all the related object indicates a Entitystate.Deleted state.
Like an update, insert or common delete, until we commit the changes with SaveChanges function, the data would not be commited.
Si habilitamos el borrado en cascada de una relación, e invocamos a la función DeleteObject del conjunto, podremos observar que todas las entidades de Detalle (de la relación indicada) presentan el valor EntityState.Deleted en la propiedad EntityState.
Del mismo modo que en el borrado, inserción o actualización, hasta que no se invoque al método SaveChanges, los cambios no van a ser confirmados en la Base de Datos.
Finally
In this chapter we have seen how to update a Entity, how to delete an Entity and how to implement Cascade Deleting through EF.
In next chapters we will see how to query the DB data.