開放源碼的對象關係映射工具ORM.NET 刪除資料 Deleting Records using ORM.NET

來源:互聯網
上載者:User
刪除一筆記錄 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 deleteddm.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 deleteif (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 recordsStudent student = dm.GetStudent(FetchPath.Student.Contact);student.Contact.Delete();      // mark the Parent Contact record to be deletedstudent.Delete();              // mark Student record Root object to be deleteddm.CommitAll();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.