开放源码的对象关系映射工具ORM.NET 删除数据 Deleting Records using ORM.NET

删除一笔记录 Object].Delete()

下面的代码,删除FirstName为Tim,LastName为Brown的学生

DataManager dm = new DataManager(Config.Dsn);
dm.QueryCriteria.Clear();
dm.QueryCriteria.And(JoinPath.Student.Columns.FirstName,”Tim”)
                .And(JoinPath.Student.Columns.LastName,”Brown”);
Student s = dm.GetStudent(FetchPath.Student);
s.Delete(); // marks the returned DataRow to be deleted
dm.CommitAll();  // performs the necessary insert,update and delete operations

删除多行记录

DataManager dm = new DataManager(Config.Dsn);
dm.QueryCriteria.And(JoinPath.Student.Columns.FirstName,"Tim")
                      .And(JoinPath.Student.Columns.LastName,"Brown");
StudentCollection students = dm.GetStudentCollection();
// check to ensure that there are record(s) to delete
if (students != null) 
{
         foreach (Student s in students)
               s.Delete(); // loop through and mark for deletion 
        dm.CommitAll();  // Delete all datarows marked for deletion transitionally
}
 

先取出数据到Collection中,再用object.Delete标记为删除,在CommitAll方法中执行删除记录

 

删除主从表记录 Delete Parent and Child record(s)

请看代码,先读取Brown学生的数据和它联系方式,之后再删除联系方式和Brown学生记录。

dm.QueryCriteria.Clear();
dm.QueryCriteria.And(JoinPath.Student.Columns.LastName,"Brown");
// Get[Object] will retrieve Student and related Contact records
Student student = dm.GetStudent(FetchPath.Student.Contact);
student.Contact.Delete();      // mark the Parent Contact record to be deleted
student.Delete();              // mark Student record Root object to be deleted
dm.CommitAll();
posted @ 2011-09-16 15:09  信息化建设  阅读(662)  评论(0编辑  收藏  举报