Directory
Write in front
Documentation and series articles
Add data
Delete data
modifying data
Add Modification Data
Summarize
Write in front
The previous article introduced the NHibernate-based conditional query on object-oriented. For a project, adding and removing changes is essential, although the implementation of different ways, but there are always involved in this part of the code. Before with a friend said a joke, you will add and delete change, all problems are not problems, in addition to business processing basically completed a project. It may be a bit too much, but I think in coding, the important thing is that your business processing ability, in the company few people can stand in the project framework or the structure of the decision-making position, this time, as a development is not responsible for the module? And these modules, if you notice will find, everywhere the addition and deletion change, and look basically the same.
Child shoes that use the eentity Framework or LINQ to SQL know that when you delete, modify, and add, these changed data are not immediately deleted from the database, but in memory until you use the SaveChange () of the database context method or SubmitChanges () submits the data to the database, which either commits or rolls back.
In NHibernate, the data is also in memory, until you call ISession's Flush () method to commit the data to the database (success or rollback (this rollback is different from the rollback of the transaction, after the next article learned the transaction, and then Compare)).
Documentation and series articles
[Nhibernate] Architecture
[NHibernate] Isessionfactory Configuration
[NHibernate] Persistence class (persistent Classes)
[NHibernate] O/R Mapping Basics
[NHibernate] Collection Class (collections) mappings
[NHibernate] Association mappings
[NHibernate] Parent/child
[NHibernate] Cache (nhibernate.caches)
[NHibernate] NHibernate.Tool.hbm2net
[NHibernate] Nullables
[NHibernate] NHibernate How to map image fields in SQL Server
[NHibernate] Basic configuration and testing
[NHibernate] HQL Query
[NHibernate] Conditional query criteria
Add data
Create a Customer object, and then call ISession's Save method to add to memory, call ISession's Flush method to the database, and the code is as follows:
1 /// <summary>2 ///Add Customer3 /// </summary>4 /// <param name= "Customer" >Customer Entity</param>5 /// <returns>whether to add success</returns>6 Public BOOLAddcustomer (Customer customer)7 {8 9 TryTen { OneNhibernatehelper Nhibernatehelper =NewNhibernatehelper (); A varSession =nhibernatehelper.getsession (); - //write the Customer object to memory - session. Save (customer); the //Update to Database - session. Flush (); - return true; - } + Catch(Exception ex) - { + Throwex; A } at}
A friend familiar with IO operations should find out that when writing a byte stream to a file, if you do not flush or close (dispose), you will find that the file being written is not content. The In-memory byte stream is written to the file only after flush or close.
Delete data
Description: Gets the object to be deleted, and then deletes the object, with the following code:
1 /// <summary>2 ///Delete Customer Information3 /// </summary>4 /// <param name= "Customer" >Customer Object</param>5 /// <returns>whether to delete the success</returns>6 Public BOOLDeleteCustomer (Customer customer)7 {8 Try9 {TenNhibernatehelper Nhibernatehelper =NewNhibernatehelper (); One varSession =nhibernatehelper.getsession (); A //Remove A persistent instance from the Datastore - session. Delete (customer); - session. Flush (); the return true; - } - Catch(Exception) - { + Throw; - } +}
modifying data
Description: Modifies customer information according to the new client entity passed in. The code is as follows:
1 /// <summary>2 ///Modify Customer Information3 /// </summary>4 /// <param name= "Customer" >Customer Object</param>5 /// <returns>whether the modification was successful</returns>6 Public BOOLUpdateCustomer (Customer customer)7 {8 Try9 {TenNhibernatehelper Nhibernatehelper =NewNhibernatehelper (); One varSession =nhibernatehelper.getsession (); A //Update The persistent instance with the identifier of the given transient instance. - session. Update (customer); - session. Flush (); the return true; - } - Catch(Exception) - { + Throw; - } +}
Add Modification Data
There is a method in NHibernate that modifies if the object exists, and adds if it does not exist.
ISession can identify the different objects and provide us with the Isession.saveorupdate (object) method.
The Isession.saveorupdate (object) method completes the following work:
-
- Check if the object already exists in the session.
- If the object is not present, call Save (object).
- If the object exists, check to see if the object has changed.
- If the object changes, call Update (object).
The code is as follows:
1 /// <summary>2 ///Add or modify customer information3 /// </summary>4 /// <param name= "Customer" >Customer Object</param>5 /// <returns>whether to modify or add success successfully</returns>6 Public BOOLSaveorupdatecustomer (Customer customer)7 {8 Try9 {TenNhibernatehelper Nhibernatehelper =NewNhibernatehelper (); One varSession =nhibernatehelper.getsession (); A //either Save () or Update () the given instance, depending upon the value of - //Its identifier property. - session. Saveorupdate (customer); the session. Flush (); - return true; - } - Catch(Exception) + { - Throw; + } A}
In the project, the Saveorupdate () method is the most used in modifying or adding data. It is also very convenient to use, add or update, let the program to judge.
Test, add a new user, and then go to modify the user information.
1 /// <summary>2 ///Add customer Information3 /// </summary>4 /// <param name= "Sender" ></param>5 /// <param name= "E" ></param>6 protected voidbtnAdd_Click (Objectsender, EventArgs e)7 {8Guid Guidcustomerid =Guid.NewGuid ();9 varCustomer =NewCustomer () {CustomerName ="Zhangsan", customeraddress ="Beijing Haidian", CustomerID =Guidcustomerid};TenBusiness.customerbusiness customerbusiness =Newbusiness.customerbusiness (); One //Add if the customer information does not exist A if(Customerbusiness.saveorupdatecustomer (customer)) - { -Customer =NewCustomer () {CustomerName ="Wanger", customeraddress ="Shanghai", CustomerID =Guidcustomerid}; the //Modification of customer information is present - if(Customerbusiness.saveorupdatecustomer (customer)) - { - Repeaterdatabind (); + } - } +}
Results
The generated SQL statement
Summarize
This article introduces the method of adding and deleting, the most used in the project is also these methods, such as you define an interface, the interface can be defined in these four ways.
[NHibernate] additions and deletions to change the operation