Through tutorial 2, we know how to configure the ing between objects and tables. The next tutorial is how to add, delete, modify, and query objects and how to operate on object addition, deletion, modification, and query.
First, explain the principle of adding, deleting, modifying, and querying objects. here we need to introduce the concept of the object state, namely, PersistentState, which indicates the behavior of the Persistent Object to operate the database.
[Dict ("actions of persistent objects on Database Operations")]
[Serializable]
Public enum PersistentState
{
/// <Summary>
/// Indicates that a new record is added to the database (default status ).
/// </Summary>
[DictEntry ("indicates adding a record to the database (default status)")]
Added = 0,
/// <Summary>
/// Indicates modifying a record to the database.
/// </Summary>
[DictEntry ("indicates modifying a record to the Database")]
Modified = 1,
/// <Summary>
/// Delete, which is not in the database and is called a free state.
/// </Summary>
[DictEntry ("delete, originally not in the database, called this status as free")]
Detached = 2,
/// <Summary>
/// Delete, which originally exists in the database.
/// </Summary>
[DictEntry ("delete, originally stored in Database")]
Deleted = 4,
}
This mechanism refers to the row State mechanism of Microsoft DataRow, and the system automatically adjusts the state of the object.
We can see from the picture on the left above that the status of the just-instantiated object is new, that is, the database operation should be saved to the database at this time.
After saving, you can perform the following actions:
1. Save again: Some applications do not close the current window after clicking the Save button. Instead, they modify some items and then click Save. At this time, the system intelligently changes the state of the object to be modified without human operations.
2. Close the current window, reload the object to it, modify the information, and click Save. When the object is loaded from the database, the state of the object is changed, whether the modification operation will be executed or not depends on a value (whether the object has been updated before it is loaded and saved). If the value is false, it does not actually update the operation. This value is the value returned by calling the BaseEo. HasChanges method.
3. Close the current window, load the object, and delete it. In this case, the system deletes the current object from the database, and changes the state of the object to the Free State.
OK. Let's take a look at how to operate the Code:
// Create a data access request for the TestEo object
BaseDao <TestEo> dao = new BaseDao <TestEo> ();
// Instantiate an object
TestEo eo = new TestEo ();
Eo. A1 = 1;
// Save
Dao. SaveOrUpdate (eo );
// Load the saved object
Eo = dao. Load ("select * from TestEo where A1 =" + eo. A1 );
Eo. A2 = 3 M;
// Update
Dao. SaveOrUpdate (eo );
// Load the updated object
Eo = dao. Load ("select * from TestEo where A1 =" + eo. A1 );
// Delete
Dao. Delete (eo );
So far, we have learned how to add, delete, modify, and query a single object.
Next, let's take a look at how to operate an object set: EoList.
What is an object set? What does it do? Why should we introduce an object set?
Suppose we have encountered such a scenario:
That is to say, before clicking the "save column design" button, all data will reside in the memory. Although addition or deletion affects the list data we see, it does not interact with the database until it is saved, and the system automatically determines which data should be saved, which should be updated and which should be deleted. This introduces another value of the object state: Deleted, which indicates that the object from the database is Deleted, marked, and Deleted when the object set is saved.
The operations for adding, deleting, and modifying a single object in an object set are quite different. When a single object is to be deleted, the Delete method is called directly, the entity set has only one operation method SaveOrUpdateOrDelete. The following is an example of calling an object set:
// Create a data access request for the TestEo object
BaseDao <TestEo> dao = new BaseDao <TestEo> ();
// Create an object collection container that contains three objects
EoList <TestEo> eoList = new EoList <TestEo> ();
EoList. Add (dao. Load ("select * from TestEo Where ID = 2 "));
EoList. Add (dao. Load ("select * from TestEo Where ID = 3 "));
// Update the second object
EoList [1]. A2 = 3 M;
// Delete the first object. The SetUnavailable method here is to change the object state to Deleted.
EoList [0]. SetUnavailable ();
// New Entity
EoList. Add (new TestEo (){});
Dao. SaveOrUpdateOrDelete (eoList );
OK. The second tutorial is complete. I hope my fellow citizens will test it more. After all, I will practice the truth. If you have any questions, join the group: 222515272
Xiangyi software, All Rights Reserved