Objeleri üzerinde “update” ve “delete” işlemlerini , objeyi “id’si ile new’lemeden” dolayısıyla, Veri tabanına bir “select” atmaya gerek duymadan yapmanın bir yolu var.
Silme işleminde hiç bir sıkıntı yok. Hatta tüm silmeleri böyle yapmamız gerekiyor diyebilirim. Fakat güncelleme’de “re-factor” yaparken dikkat edilecek bir husus var. Eğer new’lenen objede, güncellediğiniz alanlar dışında başka bir alandan bilgi alıyorsanız o alan boş gelecektir, ama güncellenecek alan belli, değer belli ise, yapıştırın gitsin. Aşağıda örneği var.
Kolay gelsin.
Bildiğimiz, sevdiğimiz kullandığımız yöntem L
KpkKontTarihceHizmetEntity guncellenecekKpkKontTarihceHizmetEntity = new KpkKontTarihceHizmetEntity(55); //burda VT’te select attı. Objeji doldurdu.
transactionHelper.AddElementToTransaction(guncellenecekKpkKontTarihceHizmetEntity);
guncellenecekKpkKontTarihceHizmetEntity.tutar = 500;
guncellenecekKpkKontTarihceHizmetEntity.Save();
Güncelleyeceğimiz objenin ID’sini biliyorsak kullanacağımız, cillop gibi yöntem J
KpkKontTarihceHizmetEntity guncellenecekKpkKontTarihceHizmetEntity = new KpkKontTarihceHizmetEntity();
guncellenecekKpkKontTarihceHizmetEntity.Fields["Id"].ForcedCurrentValueWrite(55); //(llblgen’de generate işleminde readonly=false ayarlanırsa, id’te direkt atama yapılabilir)
guncellenecekKpkKontTarihceHizmetEntity.IsNew = false;
transactionHelper.AddElementToTransaction(guncellenecekKpkKontTarihceHizmetEntity);
guncellenecekKpkKontTarihceHizmetEntity.tutar = 500;
guncellenecekKpkKontTarihceHizmetEntity.Save();
Ayrıntılı Açıklama
Modifying an entity
Modifying an entity's data is just as simple and can be done in multiple ways:
- Loading an existing entity in memory, alter one or more fields (not sequenced fields) and call Save()
- Create a new entity, set the primary key values, set the IsNew to false, set one or more other fields' values and call Save(). This will not alter the PK fields.
- Via one of the UpdateMulti*() methods defined in the collection class of the entity.
Option 1 is likely the most used one, since an entity might already be in memory. As with all the suggested options, the Save() method will see that the entity isn't new, and will therefore use an UPDATE query to alter the entity's data in the persistent storage. An UPDATE query will only update the changed fields in an entity that is saved, which results in efficient queries. If no fields are changed, no update is performed. A field which is set to the same value (according to Equals()) is not marked as 'changed' (i.e. the field's IsChanged flag is not set).
If you've loaded an entity from the database into memory and you've changed one or more of its primary key fields, these fields will be updated in the database as well (except sequenced/identity columns). Changing PK fields is not recommended and changed PK fields are not propagated to related entities fetched in memory. You also can't save changed PK fields in recursive saves.
An example for code using the first method:
// [C#]
CustomerEntity customer = new CustomerEntity("CHOPS");
customer.Phone = "(605)555-4321";
customer.Save();
This will first load the Customer entity "CHOPS" into memory, alter one field, Phone, and then save that single field back into the persistent storage. Because the loading of "CHOPS" already set the primary key, we can just alter a field and call Save(). The Update query will solely set the table field related to the entity field "Phone".
Reading an entity into memory first can be somewhat inefficient, since all we need to do is an update of an entity row in the database.
Option 2 is more efficient in that it just starts an update, without first reading the data from the database. The following code performs the same update as the previous example code illustrating option 1. Even though the PK field is changed, it is not updated, because it is not previously fetched from the database.
// [C#]
CustomerEntity customer = new CustomerEntity();
customer.CustomerID="CHOPS";
customer.IsNew=false;
customer.Phone = "(605)555-4321";
customer.Save();
We have to set the primary key field, so the Update method will only update a single entity, the "CHOPS" entity. Next, we have to mark the new, empty entity object as not being new, so Save() will call the Update method, instead of the Insert method. This is done by setting the flag IsNew to false. Next is the altering of a field, in this case "Phone", and the call of Save(). This will not load the entity back in memory, but because Save() is called, it will be marked out of sync, and the next time you'll access a property of this entity's object, it will be refetched from the persistent storage. Doing updates this way can be very efficient and you can use very complex update constructs when you apply an Expression to the field(s) to update. See for more information about Expression objects for fields Field expressions and aggregates.
|
Aşağıdaki sırada olması önemlidir.
YanıtlaSil****************************************
var guncellenecekDhDaHizmetEntity = new DhDaHizmetEntity();
transactionHelper.AddElementToTransaction(guncellenecekDhDaHizmetEntity);
guncellenecekDhDaHizmetEntity.Fields["Id"].ForcedCurrentValueWrite(100);
guncellenecekDhDaHizmetEntity.FaturaId = 500;
guncellenecekDhDaHizmetEntity.IsNew = false;
guncellenecekDhDaHizmetEntity.Save();