NHibernate – 基本的 CRUD 操作
NHibernate – 基本的 CRUD 操作
在本章中,我们将介绍基本的CRUD 操作。现在我们的系统已经准备好启动,因为我们已经成功地实现了我们的域 Student 类,我们还定义了映射文件并配置了 NHibernate。我们现在可以使用一些查询来执行 CRUD 操作。
创建数据
如您所见,NHibernateDemoDB数据库中的 Student 表中没有数据。
所以要添加一些数据,我们需要执行如下所示的添加/创建操作。
using (var session = sefact.OpenSession()) { using (var tx = session.BeginTransaction()) { var student1 = new Student { ID = 1, FirstMidName = "Allan", LastName = "Bommer" }; var student2 = new Student { ID = 2, FirstMidName = "Jerry", LastName = "Lewis" }; session.Save(student1); session.Save(student2); tx.Commit(); } Console.ReadLine(); }
如您所见,我们创建了两个学生,然后调用OpenSession的 Save() 方法,然后调用BeginTransaction的 Commit() 。这是Program.cs文件中的完整实现
using NHibernate.Cfg; using NHibernate.Dialect; using NHibernate.Driver; using System; using System.Linq; using System.Reflection; namespace NHibernateDemoApp { class Program { static void Main(string[] args) { var cfg = new Configuration(); String Data Source = asia13797\\sqlexpress; String Initial Catalog = NHibernateDemoDB; String Integrated Security = True; String Connect Timeout = 15; String Encrypt = False; String TrustServerCertificate = False; String ApplicationIntent = ReadWrite; String MultiSubnetFailover = False; cfg.DataBaseIntegration(x = > { x.ConnectionString = "Data Source + Initial Catalog + Integrated Security + Connect Timeout + Encrypt + TrustServerCertificate + ApplicationIntent + MultiSubnetFailover"; x.Driver<SqlClientDriver>(); x.Dialect<MsSql2008Dialect>(); }); cfg.AddAssembly(Assembly.GetExecutingAssembly()); var sefact = cfg.BuildSessionFactory(); using (var session = sefact.OpenSession()) { using (var tx = session.BeginTransaction()) { var student1 = new Student { ID = 1, FirstMidName = "Allan", LastName = "Bommer" }; var student2 = new Student { ID = 2, FirstMidName = "Jerry", LastName = "Lewis" }; session.Save(student1); session.Save(student2); tx.Commit(); } Console.ReadLine(); } } } }
现在让我们运行这个应用程序,然后转到 SQL Server 对象资源管理器并刷新您的数据库。您将看到上面的两个学生现在已添加到 NHibernateDemoDB 数据库的 Student 表中。
从学生表中读取数据
您可以看到,现在我们的学生表中有两条记录。要从表中读取这些记录,我们需要调用OpenSession的CreateCriteria(),如下面的代码所示。
using (var session = sefact.OpenSession()) { using (var tx = session.BeginTransaction()) { var students = session.CreateCriteria<Student>().List<Student>(); foreach (var student in students) { Console.WriteLine("{0} \t{1} \t{2}", student.ID,student.FirstMidName, student.LastName); } tx.Commit(); } Console.ReadLine(); }
所以如果你想要记录列表,那么我们可以简单地说学生类型的列表。
现在对所有学生使用foreach并说在控制台上打印 ID、FirstMidName和LastName。现在,让我们再次运行此应用程序,您将在控制台窗口中看到以下输出。
1 Allan Bommer 2 Jerry Lewis
您还可以通过使用以下代码在 OpenSession的Get()方法中指定 ID 来检索任何记录。
using (var session = sefact.OpenSession()) { using (var tx = session.BeginTransaction()) { var students = session.CreateCriteria<Student>().List<Student>(); foreach (var student in students) { Console.WriteLine("{0} \t{1} \t{2}", student.ID, student.FirstMidName, student.LastName); } var stdnt = session.Get<Student>(1); Console.WriteLine("Retrieved by ID"); Console.WriteLine("{0} \t{1} \t{2}", stdnt.ID, stdnt.FirstMidName, stdnt.LastName); tx.Commit(); } Console.ReadLine(); }
现在,当您运行应用程序时,您将看到以下输出。
1 Allan Bommer 2 Jerry Lewis Retrieved by ID 1 Allan Bommer
更新记录
要更新表中的记录,我们需要先获取该特定记录,然后通过调用OpenSession的Update()方法更新该记录,如下面的代码所示。
using (var session = sefact.OpenSession()) { using (var tx = session.BeginTransaction()) { var students = session.CreateCriteria<Student>().List<Student>(); foreach (var student in students) { Console.WriteLine("{0} \t{1} \t{2}", student.ID, student.FirstMidName, student.LastName); } var stdnt = session.Get<Student>(1); Console.WriteLine("Retrieved by ID"); Console.WriteLine("{0} \t{1} \t{2}", stdnt.ID, stdnt.FirstMidName, stdnt.LastName); Console.WriteLine("Update the last name of ID = {0}", stdnt.ID); stdnt.LastName = "Donald"; session.Update(stdnt); Console.WriteLine("\nFetch the complete list again\n"); foreach (var student in students) { Console.WriteLine("{0} \t{1} \t{2}", student.ID, student.FirstMidName, student.LastName); } tx.Commit(); } Console.ReadLine(); }
现在,当您运行应用程序时,您将看到以下输出。
1 Allan Bommer 2 Jerry Lewis Retrieved by ID 1 Allan Bommer Update the last name of ID = 1 Fetch the complete list again 1 Allan Donald 2 Jerry Lewis
如您所见,ID 等于 1 的姓氏从 Bommer 更新为 Donald。
删除记录
要从表中删除任何记录,我们需要首先获取该特定记录,然后通过调用OpenSession的Delete()方法删除该记录,如下面的代码所示。
using (var session = sefact.OpenSession()) { using (var tx = session.BeginTransaction()) { var students = session.CreateCriteria<Student>().List<Student>(); foreach (var student in students) { Console.WriteLine("{0} \t{1} \t{2}", student.ID, student.FirstMidName, student.LastName); } var stdnt = session.Get<Student>(1); Console.WriteLine("Retrieved by ID"); Console.WriteLine("{0} \t{1} \t{2}", stdnt.ID, stdnt.FirstMidName, stdnt.LastName); Console.WriteLine("Delete the record which has ID = {0}", stdnt.ID); session.Delete(stdnt); Console.WriteLine("\nFetch the complete list again\n"); foreach (var student in students) { Console.WriteLine("{0} \t{1} \t{2}", student.ID, student.FirstMidName, student.LastName); } tx.Commit(); } Console.ReadLine(); }
现在,当您运行应用程序时,您将看到以下输出。
1 Allan Donald 2 Jerry Lewis Retrieved by ID 1 Allan Bommer Delete the record which has ID = 1 Fetch the complete list again 2 Jerry Lewis
如您所见,ID 等于 1 的记录在数据库中不再可用。您还可以在 SQL Server 对象资源管理器中查看数据库。